#C5299. Subset Sum Problem

    ID: 48932 Type: Default 1000ms 256MiB

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:

  1. The first line contains an integer n representing the number of elements in the array.
  2. The second line contains n space-separated integers representing the elements of the array.
  3. 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.

## sample
6
3 34 4 12 5 2
9
2 4 3

</p>