Dinosaur Bones Digging

시간 제한5초메모리 제한2048 MB

요약
구간 질의가 주어질 때 한 구간에서 원소 m을 골라 a[m]과 그 구간에서 m보다 큰 원소 개수의 곱을 최대로 만들고, 전체 최댓값을 출력한다.
난이도

어려움10점 중 8점

유형
세그먼트 트리, 분할 정복, 정렬, 배열
정답자
아직 제출이 없습니다

문제

Paleontologists are looking for dinosaur bones! They have already found a long line of nn square sectors, and numbered them by integers from 11 to nn. Each square has a side of 11 meter. Preliminary measurements showed that, in sector ii, the depth of the soil potentially containing dinosaur bones is a_ia\_i meters. Below that depth lies solid bedrock. All the numbers a_ia\_i turned out to be different integers.

Scientists have prepared qq different plans for their research. Each plan includes the construction of a research station on a subsegment of sectors numbered from ℓ_j\ell\_j to r_jr\_j. After picking a subsegment, they will pick one of its sectors mm (ℓ_j≤m≤r_j\ell\_j \le m \le r\_j) as the main sector.

A special device will be buried in the main sector at the depth of a_ma\_m meters. This device allows the researchers to analyze the top a_ma\_m meters of all the sectors under the research station that have depth strictly greater than a_ma\_m. In total, a_m⋅ka\_m \cdot k cubic meters of soil will be analyzed, where kk is the number of sectors under the station (that is, between ℓ_j\ell\_j and r_jr\_j, 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 nn, the number of sectors (1≤n≤1061 \leq n \leq 10^6).

The second line contains nn distinct integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n, the depths of sectors (1≤a_i≤1091 \leq a\_i \leq 10^9).

The next line contains a single integer qq, the number of plans (1≤q≤1061 \leq q \leq 10^6).

Each of the next qq lines describes a plan. The jj-th of them contains two integers ℓ_j\ell\_j and r_jr\_j which are the endpoints of the subsegment for the jj-th plan (1≤ℓ_j≤r_j≤n1 \leq \ell\_j \leq r\_j \leq n).

출력

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 3⋅3=93 \cdot 3 = 9 (since 55, 77, 44 are larger than 33) cubic meters of soil will be analyzed.

예제1

  1. 예제 1

    입력
    6
    3 5 2 7 4 6
    2
    1 5
    3 6
    
    예상 출력
    9