Two teams with the smallest ability gap
InterviewTime limit2sMemory limit512 MB
Split N people into two nonempty teams so the difference between the two teams' pairwise ability sums is minimized, and print that minimum.
- Level
Medium6 of 10
- Topics
- Bit manipulation, Brute force, Array, Combinatorics
- Solved
- No attempts yet
Problem
people gather to play football. Split them into the Start team and the Link team. The two teams do not have to be the same size, but each team must hold at least one person.
The people are numbered 1 to , and an ability table is known. is the ability added to a team when person and person are on that team together. The ability of a team is the sum of over every ordered pair of two distinct members. and can differ, and when person and person are on the same team, both and are added to that team's ability.
Consider with the table below.
If persons 1 and 2 are the Start team and persons 3 and 4 are the Link team, the Start team's ability is and the Link team's ability is . If persons 1 and 3 are the Start team and persons 2 and 4 are the Link team, the Start team has and the Link team has .
The goal is to make the difference between the two team abilities as small as possible. For the table above, persons 1 and 4 on the Start team and persons 2 and 3 on the Link team give both teams an ability of 6, so the difference is 0, and no split does better.
Input
The first line contains . ()
Each of the next lines holds integers describing . The -th number on the -th line is . is always 0, and every other is an integer between 1 and 100.
Output
Print the smallest possible difference between the ability of the Start team and the ability of the Link team.