직선 위 각 구간의 강도를 받아, 여러 구간 질의마다 a<b<c와 b-a≤c-b를 만족하며 세 지점의 강도 합이 최대가 되는 값을 구한다.
어려움9동적 계획법분할 정복세그먼트 트리구현아직 제출이 없습니다시간 제한2초메모리 제한512 MBThere is a very long straight road, which consists of N sections numbered from 1 through N. Each section has specific firmness, and the firmness of the section i (1 ≤ i ≤ N) is Ai.
JOI-kun, the gifted sport star, is going to play triple jump. A triple jump consists of three consecutive jumps. Let a, b, c be the numbers of sections at which JOI-kun takes off, then the following conditions should be met.
JOI-kun is going to perform Q triple jumps. In the j-th (1 ≤ j ≤ Q) triple jump, he should take off at sections whose numbers are in the range of Lj to Rj. In other words, Lj ≤ a < b < c ≤ Rj must be hold.
JOI-kun wants to take off at firmer sections. For each triple jump, JOI-kun is curious to know the maximum sum of firmness of the sections at which JOI-kun takes off.
Write a program that, given the number of sections and the information of triple jumps, calculates for each triple jump the maximum sum of firmness of the sections at which JOI-kun takes off.
Read the following data from the standard input. All the values in the input are integers.
N
A1 A2 · · · AN
Q
L1 R1
L2 R2
. . .
LQ RQ
Write Q lines to the standard output. The j-th (1 ≤ j ≤ Q) line should contain the maximum sum of firmness of the sections at which JOI-kun takes off in the j-th triple jump.