#C5820. Subset Sum Problem
Subset Sum Problem
Subset Sum Problem
You are given an array of n integers and a target sum \( S \). Your task is to find a nonempty subset of the array whose elements sum exactly to \( S \). If such a subset exists, output the elements of the subset in the order they appear in the array; otherwise, output -1.
Note: Each number in the array can be used at most once. If there are multiple solutions, output any one of them.
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains two integers \( n \) and \( S \): the size of the array and the target sum, respectively.
- The second line contains \( n \) space-separated integers, representing the elements of the array.
outputFormat
Output the subset (as space‐separated integers) that sums to \( S \) via standard output (stdout). If no valid subset exists, output -1.
## sample5 9
3 34 4 12 5
4 5