Lensu has a very large garden and wants to water all plants lined up next to his fountain. To prevent them from withering by the evening, each plant must receive at least 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 , and if there is no plant to the right with the above property, the robot will water up to plant .
In the morning, Lensu will give the robot data of the type:
- the robot's stopping location
- 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 , the second line contains the required amounts for each plant, the third line contains the number , representing the number of data, and the next lines contain two numbers and , 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.
- For points,
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 to position , for seconds (the closest plant to the left with a smaller capacity than the capacity of plant 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 is plant number , with a capacity of ).
The robot will execute the second command and similarly, after the presented algorithm, will water the plants from position to position , for one second.
The robot will execute the third command and will water the plants from position to position , for seconds.
The robot will execute the last command and will water the plants from position to position , for seconds.
At the end of the operations, each plant will be watered for:
seconds.
It is observed that drops are wasted, which we can redistribute to save a maximum of plants.