Robotul gospodar

Time limit: 0.25s Memory limit: 64MB Input: Output:

Lensu has a very large garden and wants to water all NN plants lined up next to his fountain. To prevent them from withering by the evening, each plant i (i1,2,,N)i \ (i ∈ {1, 2, \dots, N}) must receive at least cic_i drops of water. Lensu does not want to waste time, so he asks the robotics team to help him build a robot that will do all the work for him.

Although they worked a lot on the robot, its algorithm is quite peculiar. From its current position, our robot will water with one drop per second of staying, all plants in the interval bounded by the closest plant to the left with a required amount strictly less than the required amount of the current plant, and the closest plant to the right with a required amount strictly greater than the required amount of the current plant. If there is no plant to the left with the above property, the robot will water up to plant 11, and if there is no plant to the right with the above property, the robot will water up to plant NN.

In the morning, Lensu will give the robot KK data of the type:
XiX_i - the robot's stopping location
TiT_i - how long (in seconds) it will stay there

Task

Lensu, passionate about algorithms, suspects that the robot is not as efficient as he would like it to be and asks for your help to answer the following question:
Knowing that the robot does not know when the plant's capacity is reached, thus continuing to water the plant if it is within its range, what is the maximum number of plants Lensu could save from withering if he redistributes the wasted drops, watering other plants?

Input Data

The first line contains the number NN, the second line contains the NN required amounts for each plant, the third line contains the number KK, representing the number of data, and the next KK lines contain two numbers XiX_i and TiT_i, as described in the task. The numbers on the same line are separated by a space.

Output Data

The first line will contain a single number, the answer to the question.

Constraints and Clarifications

  • While moving from one place to another, the robot does not water any plants.
  • N300,000N \leq 300,000
  • K150,000K \leq 150,000
  • ci1,000,000, (i1,2,...,N)c_i \leq 1,000,000, \ (i ∈ {1, 2, ..., N})
  • Ti1,000,000, (i1,2,...,K)T_i \leq 1,000,000, \ (i ∈ {1, 2, ..., K})
  • 1XiN, (i1,2,...,K)1 \leq X_i \leq N, \ (i ∈ {1, 2, ..., K})
  • For 2020 points, 1N2001 \leq N \leq 200

Note

Because there is a lot of input data which has to be read, we recommend using the following instructions at the beginning of the code:

ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);

Example

stdin

10
3 2 5 1 8 0 4 10 5 3              
4
4 3
7 1
5 2
1 3

stdout

4

Explanation

The robot will execute the first command and will water the plants from position 11 to position 55, for 33 seconds (the closest plant to the left with a smaller capacity than the capacity of plant 44 does not exist, so it will start watering from the first plant, and the closest plant to the right with a larger capacity than the capacity of plant 44 is plant number 55, with a capacity of 88).

The robot will execute the second command and similarly, after the presented algorithm, will water the plants from position 66 to position 88, for one second.

The robot will execute the third command and will water the plants from position 44 to position 88, for 22 seconds.

The robot will execute the last command and will water the plants from position 11 to position 33, for 33 seconds.

At the end of the operations, each plant will be watered for:
6 6 6 5 5 3 3 3 0 06 \ 6 \ 6 \ 5 \ 5 \ 3 \ 3 \ 3 \ 0 \ 0 seconds.

It is observed that 1515 drops are wasted, which we can redistribute to save a maximum of 44 plants.

Log in or sign up to be able to send submissions!