#C5299. Subset Sum Problem
Subset Sum Problem
Subset Sum Problem
You are given an array of positive integers and a target sum \( S \). Your task is to find a subset of the array whose sum equals \( S \). If such a subset exists, output the subset as a sequence of space-separated integers (in the order in which they appear in the array). If no such subset exists, output -1
.
Note: If there are multiple valid subsets, you may output any one of them.
inputFormat
The input is read from standard input as follows:
- The first line contains an integer n representing the number of elements in the array.
- The second line contains n space-separated integers representing the elements of the array.
- The third line contains an integer S which is the target sum.
outputFormat
Output to standard output a single line. If a subset exists whose elements sum to \( S \), print the elements of one such subset separated by a single space. If no such subset exists, print -1
.
6
3 34 4 12 5 2
9
2 4 3
</p>