This page is still under construction.

Parts of this page are still being built. What you see may change.

Smallest unreachable subsequence sum

Time limit2sMemory limit512 MB

Summary
For each subarray, find the smallest non-negative integer that no subsequence sums to.
Level

Hard9 of 10

Topics
Segment tree, Greedy, Sorting
Solved
No attempts yet

Problem

You are given a sequence AA of length NN. A subsequence is what is left after deleting some elements of AA, and deleting every element is allowed, so the empty sequence is a subsequence too. The sum of a subsequence is the sum of the integers it keeps, and the sum of the empty subsequence is 0.

For example, if AA is [1, 1, 3, 7], the subsequences [], [1], [1, 1], [3], [1, 3], [1, 1, 3] have sums 0, 1, 2, 3, 4, 5 in that order. No subsequence sums to 6, so the smallest non-negative integer that is not a subsequence sum of AA is 6.

Then MM queries are given. Each query consists of two integers LL and RR and asks for the smallest non-negative integer that is not the sum of any subsequence of AL,AL+1,…,ARA_L, A_{L+1}, \dots, A_R. Write a program that answers every query.

Input

The first line contains the length of the sequence NN (1≤N≤1000001 \le N \le 100000). The second line contains the sequence A1,A2,…,ANA_1, A_2, \dots, A_N. Every number is a natural number at most 10910^9, and the sum of all numbers in the sequence is also at most 10910^9.

The third line contains the number of queries MM (1≤M≤1000001 \le M \le 100000). Each of the next MM lines contains one query in the form LiL_i RiR_i (1≤Li≤Ri≤N1 \le L_i \le R_i \le N).

Output

Print the answer to each query on its own line, in the order the queries are given.

Examples8

  1. Example 1

    Input
    5
    1 2 4 9 10
    5
    1 1
    1 2
    1 3
    1 4
    1 5
    
    Expected output
    2
    4
    8
    8
    8
    
  2. Example 2

    Input
    4
    1 1 3 7
    6
    1 4
    1 3
    2 4
    3 4
    4 4
    2 2
    
    Expected output
    6
    6
    2
    1
    1
    2
    
  3. Example 3

    Input
    1
    1
    1
    1 1
    
    Expected output
    2
    
  4. Example 4

    Input
    1
    1000000000
    1
    1 1
    
    Expected output
    1
    
  5. Example 5

    Input
    10
    1 1 1 1 1 1 1 1 1 1
    8
    1 1
    1 10
    3 7
    5 5
    2 3
    1 9
    10 10
    4 10
    
    Expected output
    2
    11
    6
    2
    3
    10
    2
    8
    
  6. Example 6

    Input
    29
    1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456
    8
    1 29
    1 1
    2 29
    1 28
    5 10
    29 29
    1 20
    10 29
    
    Expected output
    536870912
    2
    1
    268435456
    1
    1
    1048576
    1
    
  7. Example 7

    Input
    6
    5 3 2 7 2 4
    6
    1 6
    2 2
    3 3
    1 3
    4 6
    2 5
    
    Expected output
    1
    1
    1
    1
    1
    1
    
  8. Example 8

    Input
    8
    1 1 1 100 1 2 4 1
    8
    1 8
    4 4
    1 3
    4 8
    1 4
    5 8
    2 7
    3 6
    
    Expected output
    12
    1
    4
    9
    4
    9
    10
    5