#C2134. Combination Sum

    ID: 45417 Type: Default 1000ms 256MiB

Combination Sum

Combination Sum

Given a list of distinct integers and a target integer \(T\), find all unique combinations of the integers where the chosen numbers sum to \(T\). Each number in the list may be used an unlimited number of times.

Note:

  • The solution set must not contain duplicate combinations.
  • All numbers (including \(T\)) are positive integers.

For example, when the input list is [2, 3, 6, 7] and \(T = 7\), the valid combinations are:

2 2 3
7

inputFormat

The input is provided via standard input (stdin) with the following format:

  1. The first line contains an integer \(n\) representing the number of available numbers.
  2. If \(n > 0\), the second line contains \(n\) space-separated positive integers.
  3. The next line contains the target integer \(T\).

If \(n = 0\), there will be no list of numbers and the target immediately follows.

outputFormat

Output all unique combinations where the sum of numbers equals \(T\). Each combination should be printed on a separate line with numbers separated by a single space. The combinations must be printed in lexicographical order based on their numerical values. If no valid combination exists, produce no output.

## sample
4
2 3 6 7
7
2 2 3

7

</p>