#abc375e. E - 3 Team Division
E - 3 Team Division
Score : points
问题陈述
有 个人被分成三个队伍。
这些人被编号为 ,队伍被编号为 。目前,第 个人属于队伍 。
每个人都有一个称为 力量 的值;第 个人的力量是 。一个队伍的 力量 被定义为其成员力量的总和。
确定是否可能让零个或更多的人换队伍,以便所有队伍的力量相等。如果可能,找出需要换队伍的最少人数以实现这一点。
你不能创建除了队伍 、、 之外的新队伍。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
There are people divided into three teams.
The people are numbered , and the teams are numbered . Currently, person belongs to team .
Each person has a value called strength; person has a strength of . The strength of a team is defined as the sum of the strengths of its members.
Determine whether it is possible for zero or more people to switch teams so that all teams have equal strength. If it is possible, find the minimum number of people who need to switch teams to achieve this.
You cannot create new teams other than teams , , .
Constraints
- For each , there exists some with .
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If it is possible to make all teams have equal strength, print the minimum number of people who need to switch teams. Otherwise, print -1
.
Sample Input 1
6
1 2
2 5
1 5
3 3
1 3
3 6
Sample Output 1
2
If person switches to team and person switches to team , all teams will have a strength of .
Sample Input 2
4
1 1
1 2
2 3
3 4
Sample Output 2
-1
Sample Input 3
3
1 1
2 1
3 1
Sample Output 3
0
Sample Input 4
12
2 5
1 4
3 3
2 3
3 9
1 2
2 2
3 9
2 6
1 9
1 1
3 1
Sample Output 4
3