Smallest unreachable subsequence sum
Time limit2sMemory limit512 MB
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 of length . A subsequence is what is left after deleting some elements of , 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 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 is 6.
Then queries are given. Each query consists of two integers and and asks for the smallest non-negative integer that is not the sum of any subsequence of . Write a program that answers every query.
Input
The first line contains the length of the sequence (). The second line contains the sequence . Every number is a natural number at most , and the sum of all numbers in the sequence is also at most .
The third line contains the number of queries (). Each of the next lines contains one query in the form ().
Output
Print the answer to each query on its own line, in the order the queries are given.