#P4587. [FJOI2016] 神秘数

[FJOI2016] 神秘数

Description

The mysterious number of a multiset SS is defined as the smallest positive integer that cannot be represented as the sum of a submultiset of SS. For example, with S={1,1,1,4,13}S = \{1,1,1,4,13\}, we have: 1=11 = 1, 2=1+12 = 1+1, 3=1+1+13 = 1+1+1, 4=44 = 4, 5=4+15 = 4+1, 6=4+1+16 = 4+1+1, 7=4+1+1+17 = 4+1+1+1.

88 cannot be represented as the sum of a submultiset of SS, so the mysterious number of SS is 88.

You are given a sequence aa of length nn consisting of positive integers, and mm queries. Each query contains two parameters l,rl, r. You need to find the mysterious number of the multiset formed by al,al+1,,ara_l, a_{l+1}, \cdots, a_r.

Input Format

  • The first line contains an integer nn, the number of elements.
  • The second line contains nn positive integers a1,a2,,ana_1, a_2, \dots, a_n, indexed from 11.
  • The third line contains an integer mm, the number of queries.
  • Each of the next mm lines contains two integers l,rl, r.

Output Format

For each query, output one line with the corresponding answer.

5
1 2 4 9 10
5
1 1
1 2
1 3
1 4
1 5
2
4
8
8
8

Hint

For 100%100\% of the testdata, 1n,m1051 \le n, m \le 10^5, ai109\sum a_i \le 10^9.

Translated by ChatGPT 5