All problems

Problem 289

Combination Sum

Medium · 20 pts · Array, Backtracking

Problem

You are given N distinct positive integers and a target T. Find every combination of these numbers that adds up to T, where each number may be used any number of times. Two combinations are the same if they use the same numbers the same number of times, regardless of order. Write each combination in non-decreasing order, one per line, and list the combinations in lexicographic order. If no combination exists, print NONE. Sort the numbers and backtrack: at each step try numbers from the current one upwards (never going back to smaller ones, which avoids repeated combinations), and stop exploring when the sum exceeds T.

Input

First line: N and T. Second line: N distinct positive integers.

Output

Print one combination per line (values space-separated), or NONE.

Constraints

1 <= N <= 30; 2 <= value <= 40; 1 <= T <= 40; at most 150 combinations

Example input

4 7
2 3 6 7

Example output

2 2 3
7

Explanation

With the numbers 2, 3, 6 and 7, the target 7 can be reached as 2 + 2 + 3 or as 7 alone. In lexicographic order, 2 2 3 comes before 7.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Sudoku Solver Next: Tribonacci Number