Quake Live Team Split

Split an even number of players with given skill levels into two equal-size teams so the team totals differ as little as possible.

Medium4Dynamic programmingBrute forceInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

Quake Live is a first person shooter that supports several match types. The most popular one is the 4 on 4 team deathmatch. Eight players enter, they are split into two teams of four, and the teams fight to the death. The team whose players all die first loses.

The server keeps a match history for every player and estimates each player's skill level from it, as an integer between 1 and 1000. To keep a match as fair as possible, the server looks at the skill levels of the players who connect and splits them into two teams so that the difference between the sum of skills on team A and the sum of skills on team B is as small as possible.

You want to check that the server really does this. Given the skill levels of the players who enter, find the smallest possible difference between the two team totals. The two teams always have the same number of players.

Input

The first line contains the number of test cases TT. Each of the next TT lines describes one test case. A line starts with the number of players NN, and the remaining NN integers on that line are the skill levels of the players, given in no particular order.

Limits

  • 1T1001 \le T \le 100
  • NN is even and 2N162 \le N \le 16
  • every skill level is an integer between 1 and 1000

Output

For each test case, print one line in the form Case #X: Y, where XX is the test case number starting from 1 and YY is the smallest possible difference between the sum of skill levels on team A and the sum of skill levels on team B.