Equal Sums

Time limit20sMemory limit512 MB

Summary
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 SS of positive integers. Find two different non-empty subsets whose elements add up to the same sum.

A subset contains only elements of SS, 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, TT. Each of the next TT lines holds one test case. A test case starts with NN, the number of integers in SS, followed on the same line by the NN distinct positive integers of SS.

Limits

  • 1≤T≤101 \le T \le 10
  • 1≤N≤201 \le N \le 20
  • No two numbers in SS are equal.
  • Each number in SS is a positive integer smaller than 101210^{12}.

Output

For each test case, first print a line with Case #x:, where xx 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.

  1. Let ss be the smallest sum for which two different subsets with that sum exist.
  2. Collect every subset whose elements add up to ss, and read each one as the sequence of its elements in increasing order.
  3. 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.

Examples2

  1. Example 1

    Input
    2
    20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    20 120 266 858 1243 1657 1771 2328 2490 2665 2894 3117 4210 4454 4943 5690 6170 7048 7125 9512 9600
    
    Expected output
    Case #1:
    1 2
    3
    Case #2:
    120 2894
    1243 1771
    
  2. Example 2

    Input
    2
    3 3 1 2
    1 7
    
    Expected output
    Case #1:
    1 2
    3
    Case #2:
    Impossible