All problems

Problem 729

Combination Sum II

Medium · 20 pts · Recursion, Backtracking

Problem

Given candidate numbers (which may repeat) and a target, find every distinct combination of candidates that adds up to the target, using each candidate position at most once. Combinations with the same values in the same quantities count once. Write each combination in non-decreasing order and list them in lexicographic order. Print the number of combinations first. Sort the candidates, and while backtracking skip a value that equals the previous one at the same depth.

Input

The first line contains N and TARGET. The second line contains the N candidates.

Output

Print the number of combinations, then each combination on its own line in lexicographic order.

Constraints

1 <= N <= 30; 1 <= candidate <= 50; 1 <= TARGET <= 30

Example input

7 8
10 1 2 7 6 1 5

Example output

4
1 1 6
1 2 5
1 7
2 6

Explanation

With candidates 10 1 2 7 6 1 5 and target 8, the combinations are 1 1 6, 1 2 5, 1 7 and 2 6. The two 1s may be used together, but each 1 only once, so there are 4 combinations.

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

Previous: Subsets II Next: Combination Sum III