#C12672. Unique Combinations Sum

    ID: 42125 Type: Default 1000ms 256MiB

Unique Combinations Sum

Unique Combinations Sum

You are given a list of positive integers and a target sum \(T\). Your task is to find all unique combinations of the array elements that add up to \(T\). Each combination must be sorted in non-decreasing order and each unique combination should appear only once, even if the input array contains duplicates.

You may use an element from the array as many times as needed. Formally, given a sorted array \(A = [a_1, a_2, \dots, a_n]\) and a target \(T\), find all sequences \( [x_1, x_2, \dots, x_k] \) such that:

\(x_1 + x_2 + \dots + x_k = T, \quad x_1 \le x_2 \le \dots \le x_k\)

Output the combinations in the order they are found by a typical backtracking algorithm.

inputFormat

The input is read from standard input (stdin) and has three lines:

  1. The first line contains an integer (n) representing the number of elements in the array.
  2. The second line contains (n) space-separated positive integers.
  3. The third line contains a positive integer (T) representing the target sum.

outputFormat

Print each unique combination that sums up to (T) on a separate line. Within each line, the numbers should be separated by a single space. If no valid combination exists, output nothing.## sample

5
2 3 6 7 7
7
2 2 3

7

</p>