Equal Sums
Time limit20sMemory limit512 MB
Given up to 20 distinct numbers, print the two lexicographically smallest distinct subsets that share the smallest repeated sum, or Impossible.
- Level
Medium5 of 10
- Topics
- Brute force, Hash map, Sorting
- Solved
- No attempts yet
Problem
You are given a set of positive integers. Find two different non-empty subsets whose elements add up to the same sum.
A subset contains only elements of , and two subsets are different when their elements are not exactly the same. Neither subset may be empty. The two subsets are allowed to share elements.
Input
The first line contains the number of test cases, . Each of the next lines holds one test case. A test case starts with , the number of integers in , followed on the same line by the distinct positive integers of .
Limits
- No two numbers in are equal.
- Each number in is a positive integer smaller than .
Output
For each test case, first print a line with Case #x:, where is the test case number starting from 1.
If no two different subsets have the same sum, print Impossible on the next line.
Otherwise pick the single answer given by these rules.
- Let be the smallest sum for which two different subsets with that sum exist.
- Collect every subset whose elements add up to , and read each one as the sequence of its elements in increasing order.
- Sort those sequences lexicographically, then print the first and the second, one per line.
Print the elements of a subset in increasing order, separated by single spaces. A lexicographic comparison reads elements from the front, and when one sequence equals the beginning of the other, the shorter one comes first.