All problems

Problem 727

Subset Sums

Medium · 20 pts · Recursion, Backtracking, Bit Manipulation

Problem

Given N integers, list the sums of all 2^N subsets, including the empty subset whose sum is 0. Subsets are chosen by position, so equal values in different positions give different subsets and repeated sums are all listed. Print the sums in non-decreasing order. Recursively decide for each element whether to take it, adding the running sum to the list at the end of each branch, then sort.

Input

The first line contains N. The second line contains N integers.

Output

Print all 2^N subset sums in non-decreasing order, separated by spaces.

Constraints

1 <= N <= 15; 0 <= values <= 10^4

Example input

2
2 3

Example output

0 2 3 5

Explanation

For 2 3 the subsets are {}, {2}, {3} and {2, 3}, with sums 0, 2, 3 and 5. Sorted, the answer is 0 2 3 5.

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

Previous: Add One to a Number Represented as a Linked List Next: Subsets II