#K41087. Smallest Unrepresentable Sum

    ID: 26787 Type: Default 1000ms 256MiB

Smallest Unrepresentable Sum

Smallest Unrepresentable Sum

You are given a set of positive integers. Your task is to find the smallest positive integer that cannot be represented as the sum of a subset of these integers.

Let \(A = {a_1, a_2, \ldots, a_n}\) be the set sorted in non-decreasing order. The solution uses a greedy strategy: initialize a candidate value \(S=1\). Then, for each number \(a_i\) in the set, if \(a_i \leq S\), update \(S=S+a_i\); otherwise, \(S\) is the smallest number that cannot be represented as a subset sum. For example, for the set [1, 2, 3, 10, 20], the answer is 7 because you can represent all values from 1 to 6 but not 7.

Input is given as multiple test cases on separate lines. Each test case starts with an integer \(n\) denoting the number of elements, followed by \(n\) integers. The sequence of test cases terminates with a line containing a single 0.

Your program should process each test case, compute the smallest unrepresentable sum, and output the result for each test case on a new line.

inputFormat

Input is read from standard input. It contains multiple test cases. Each test case is on a separate line and is formatted as follows:

n a1 a2 ... an

Here, n is the number of integers in the set and a1, a2, ..., an are the positive integers. The input terminates with a line containing a single 0.

outputFormat

For each test case, output the smallest positive integer that cannot be represented as the sum of a subset of the given set. Each result should be printed on a new line.

## sample
5 1 2 3 10 20
3 5 7 1
0
7

2

</p>