Smallest Unpayable Amount
Time limit4sMemory limit512 MB
For each query interval, find the smallest positive amount that cannot be formed as a subset sum of the coins in that interval.
- Level
Hard9 of 10
- Topics
- Greedy, Sorting, Segment tree, Binary search
- Solved
- No attempts yet
Problem
Boris has collected coins. He laid them out in a row, and the -th coin in the row has value .
Boris is about to leave on a trip, but he is short of time, so he plans to take one continuous segment of adjacent coins from the row.
Boris wants to answer several queries. For each query he wants to know the smallest amount he cannot pay without receiving change, given that he takes the coins from position to position . Formally, find the smallest positive integer such that no choice of coins from position to position has values summing to .
Input
The first line contains the number of coins and the number of queries (). The second line contains integers , the values of the coins ().
Each of the next lines contains two integers and (), the first and the last position of the segment Boris takes.
Output
For each query print one integer on its own line: the smallest amount that cannot be paid without change using the coins from position to position .