#SGM0000011. Truy vấn tổng đoạn con lớn nhất I (Can you answer these queries I)

Truy vấn tổng đoạn con lớn nhất I (Can you answer these queries I)

Can you answer these queries I

Source: SPOJ

Version: Phuoc Hung OJ Extended

Given an array A1,A2,…,ANA_1,A_2,\ldots,A_N, each query x,yx,y asks for the maximum sum of a non-empty contiguous subarray fully contained in [x,y][x,y]:

max⁡x≤i≤j≤y∑k=ijAk.\max_{x\le i\le j\le y}\sum_{k=i}^{j}A_k.

Input

The first line contains NN.

The second line contains the array.

The third line contains QQ.

Each of the next QQ lines contains x,yx,y.

Output

For each query, print the maximum contiguous subarray sum.

Subtasks

  • Subtask 1 — 20%: 1≤N,Q≤501\le N,Q\le 50, ∣Ai∣≤1000|A_i|\le 1000.
  • Subtask 2 — 30%: 1≤N,Q≤50001\le N,Q\le 5000, ∣Ai∣≤15007|A_i|\le 15007.
  • Subtask 3 — 50%: 1≤N≤500001\le N\le 50000, 1≤Q≤500001\le Q\le 50000, ∣Ai∣≤15007|A_i|\le 15007.

Examples

Input

3
-1 2 3
1
1 2

Output

2

Explanation

The sample follows the operations exactly; each printed line corresponds to a query that requires output.