Link and Start 2
Time limit2sMemory limit512 MB
Given an N by N matrix of pair values, split N people into two non-empty teams to minimize the difference in their team sums.
- Level
Medium5 of 10
- Topics
- Brute force, Backtracking, Recursion
- Solved
- No attempts yet
Problem
Today, people who work at the company gather to play soccer. The game is played on weekday afternoons, and attendance is not mandatory. A total of N people have gathered to play. The people must be split into a Start team and a Link team. The two teams do not need the same number of members, but each team must have at least one person.
The company assigns each person a number from 1 to N and surveys the ability values below. The ability value is added to a team when person i and person j are on the same team. A team's ability is the sum of over all pairs in the team. may differ from , and when person i and person j are on the same team, the team gains both and .
Suppose N=4 and S is as follows.
For example, if persons 1 and 2 are on the Start team and persons 3 and 4 are on the Link team, the abilities are:
- Start team:
- Link team:
If persons 1 and 3 are on the Start team and persons 2 and 4 are on the Link team, the abilities are:
- Start team:
- Link team:
To make the game fun, the goal is to minimize the difference between the Start team ability and the Link team ability. In the case above, if persons 1 and 4 are on the Start team and persons 2 and 3 are on the Link team, both teams have ability 6, so the difference is 0, which is the minimum.
Input
The first line contains N (4 ≤ N ≤ 400). The next N lines each contain N integers, and the j-th number in the i-th line is . is always 0. Every other is an integer from 1 to 100, inclusive.
Output
Print the minimum difference between the ability of the Start team and the ability of the Link team.