Dinosaur Bones Digging
시간 제한5초메모리 제한2048 MB
구간 질의가 주어질 때 한 구간에서 원소 m을 골라 a[m]과 그 구간에서 m보다 큰 원소 개수의 곱을 최대로 만들고, 전체 최댓값을 출력한다.
문제
Paleontologists are looking for dinosaur bones! They have already found a long line of square sectors, and numbered them by integers from to . Each square has a side of meter. Preliminary measurements showed that, in sector , the depth of the soil potentially containing dinosaur bones is meters. Below that depth lies solid bedrock. All the numbers turned out to be different integers.
Scientists have prepared different plans for their research. Each plan includes the construction of a research station on a subsegment of sectors numbered from to . After picking a subsegment, they will pick one of its sectors () as the main sector.
A special device will be buried in the main sector at the depth of meters. This device allows the researchers to analyze the top meters of all the sectors under the research station that have depth strictly greater than . In total, cubic meters of soil will be analyzed, where is the number of sectors under the station (that is, between and , inclusive) which are deeper than the main sector.
Paleontologists want to find as much dinosaur bones as possible, so they want to analyze as much soil as possible. Help them! Find the maximum volume of soil which can be analyzed if a subsegment is chosen from the plans, and its main sector is then chosen optimally.
입력
The first line contains a single integer , the number of sectors ().
The second line contains distinct integers , the depths of sectors ().
The next line contains a single integer , the number of plans ().
Each of the next lines describes a plan. The -th of them contains two integers and which are the endpoints of the subsegment for the -th plan ().
출력
Print a line with a single integer: the maximum volume of analyzed soil in cubic meters.
힌트
In the example, scientists should pick the first plan and the first sector as its main sector. Then (since , , are larger than ) cubic meters of soil will be analyzed.