#K62147. Smallest Missing Positive Sum
Smallest Missing Positive Sum
Smallest Missing Positive Sum
You are given an array of n positive integers. Your task is to determine the smallest positive integer S that cannot be represented as the sum of some subset of the given array.
More formally, let the array be A = [a1, a2, ..., an]. Define the set of all possible subset sums as:
$$G = \left\{ \sum_{i\in I} a_i : I \subseteq \{1,2,\ldots,n\} \right\}. $$Your goal is to find the smallest positive integer which is not in G.
Example:
- For A = [1, 2, 2, 5, 7], the answer is
18
. - For A = [1, 1, 1], the answer is
4
.
Hint: A greedy approach is effective. Sort the array, then iteratively determine the smallest unreachable sum.
inputFormat
The first line of input contains a single integer n (1 ≤ n ≤ 105), representing the number of elements in the array.
The second line contains n space-separated positive integers.
outputFormat
Output a single integer — the smallest positive integer that cannot be represented as the sum of a subset of the given array.
## sample5
1 2 2 5 7
18
</p>