#P7503. 「HMOI R1」文化课

「HMOI R1」文化课

Description

There are nn people taking an academic proficiency exam. Since they pretend that they have retired, the afternoon CPS0202 has nothing to do with them.

Currently, person ii has a score aia_i. To pass, they need to get at least lil_i points. To avoid being suspected by the teacher, their score cannot exceed rir_i.

You may organize several cheating sessions. These sessions happen simultaneously, so no one can participate in two or more sessions at the same time. Each cheating session is carried out on a consecutive segment of examinees, and all their scores become the highest score among them.

Find the maximum number of people who can pass and still not be suspected.

Input Format

The first line contains an integer nn, meaning there are nn people.

The second line contains nn integers separated by spaces. The ii-th number aia_i represents the initial score of person ii.

The next nn lines each contain two integers. On the ii-th line are lil_i and rir_i, with meanings as described above.

Output Format

One line with one integer, representing the maximum number of people who can pass and still not be suspected by the teacher.

6
1 1 4 5 1 4
1 1
4 5
1 4
1 5
1 1
4 4

6

Hint

Organizing one cheating session on [2,3][2,3] can make everyone satisfy the conditions.

This problem uses bundled testdata.

  • Subtask 1 (55 points): n5n \le 5.
  • Subtask 2 (55 points): n100n \le 100.
  • Subtask 3 (1010 points): n8×103n \le 8 \times 10^3.
  • Subtask 4 (3030 points): li=ril_i = r_i.
  • Subtask 5 (2020 points): aiai+1a_i \le a_{i+1}.
  • Subtask 6 (3030 points): no special properties.

For 100%100\% of the data, 1n1051 \le n \le 10^5, 1ain1 \le a_i \le n, 1lirin1 \le l_i \le r_i \le n.


  • Idea: FZzzz.
  • Solution: FZzzz.
  • Code: FZzzz.
  • Data: FZzzz.

Translated by ChatGPT 5