Triple Jump
Time limit2sMemory limit512 MB
For each range [L, R], find the maximum A[a]+A[b]+A[c] over indices a<b<c inside the range where b-a <= c-b.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Segment tree, Dynamic programming
- Solved
- No attempts yet
Problem
There is a very long straight road with sections numbered from to . Section () has firmness .
JOI-kun, the gifted sport star, is going to play triple jump. A triple jump consists of three consecutive jumps. Let , , be the sections where JOI-kun takes off. The following conditions must hold.
- . The section numbers are increasing.
- . The first jump is no longer than the second jump.
JOI-kun will perform triple jumps. In the -th triple jump, he takes off from sections in the range to . That is, must hold.
JOI-kun wants to take off from firmer sections. For each triple jump, find the maximum sum of firmness of the sections at which he takes off.
Input
The input is given in the following format. All values in the input are integers.
N
A1 A2 ... AN
Q
L1 R1
L2 R2
...
LQ RQ
Output
Print lines. The -th line contains the maximum sum of firmness of the sections at which JOI-kun takes off in the -th triple jump.
Constraints
- ()
- ()