Array Game

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem types

Alice and Bob are playing an array game. Initially, each of them has an array of length N. Alice's array is A = [a_1, a_2, \dots, a_N] and Bob's array is B = [b_1, b_2, \dots, b_N], where all a_i and b_i are positive integers.

Alice may perform any number of operations (including zero). In a single operation, she chooses a contiguous interval [L, R] (1 \le L < R \le N) and replaces every element in that interval by the maximum value over that interval. Formally, she computes M = \max_{\,L \le j \le R} a_j, and then for every i (L \le i \le R) sets a_i = M.

After Alice finishes all her operations, she compares her final array A to Bob's array B. She scores one point for each index i where a_i = b_i. Compute the maximum number of points Alice can get by choosing her operations optimally.

Input Specification

The first line contains an integer N (1 \le N \le 10^5), the length of the arrays.

The second line contains N integers a_i, (1 \le a_i \le 10^9), indicating Alice's array.

The third line contains N integers b_i, (1 \le b_i \le 10^9), indicating Bob's array.

Output Specification

Print a single integer, the maximum number of points Alice can achieve.

Constraints

Subtask Points Additional constraints
1 14 N \le 10
2 16 N \le 200.
3 13 N \le 5\,000 and the array A is strictly increasing (a_1 < a_2 < \dots < a_N).
4 22 N \le 5\,000.
5 12 All b_i are the same (b_1 = b_2 = \cdots = b_N).
6 23 No additional constraints

Sample Input

4
10 2 9 2
10 9 10 9

Sample Output

3

Explanation for Sample

Alice can choose the interval [2, 4] and change the array to [10, 9, 9, 9], then she can get 3 points. There is no way to get more points.


Comments

There are no comments at the moment.