Dinosaur Bones Digging
Time limit5sMemory limit2048 MB
For each plan [l, r], the task is to pick a main sector m that maximizes a_m times the number of sectors in [l, r] deeper than a_m.
- Level
Hard9 of 10
- Topics
- Segment tree, Sorting, Array
- Solved
- No attempts yet
Problem
Paleontologists are looking for dinosaur bones! They have already found a long line of square sectors, numbered from 1 to . Each square has a side of 1 meter. Preliminary measurements showed that in sector , the depth of soil potentially containing dinosaur bones is meters. Below that depth lies solid bedrock. All the numbers are distinct integers.
Scientists have prepared research plans. Each plan builds a research station on the subsegment of sectors from to . After picking the subsegment, they pick one of its sectors () as the main sector.
A special device is buried in the main sector at a depth of meters. The device analyzes the top meters of every sector under the station whose depth is strictly greater than . In total, cubic meters of soil are analyzed, where is the number of sectors under the station (between and , inclusive) that are deeper than the main sector.
The paleontologists want to analyze as much soil as possible. Find the maximum volume of soil that can be analyzed when one plan is chosen and its main sector is then chosen optimally.
Input
The first line contains a single integer , the number of sectors ().
The second line contains distinct integers , the depths of the sectors ().
The next line contains a single integer , the number of plans ().
Each of the next lines contains two integers and , the endpoints of the subsegment for the -th plan ().
Output
Print a single integer on one line: the maximum volume of analyzed soil in cubic meters.
Hint
In the example, the scientists should pick the first plan and the first sector as its main sector. Then cubic meters of soil are analyzed, since , , and are deeper than .