MathWorksOA #20
Balancing Teams
EasyJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Teams A and B have n and m players respectively, and their skill values are stored in the arrays teamA and teamB. A value of 0 means the team is missing a player at that position.
The coach must fill every empty position with a player so that the two teams' total skill is equal. Any player's skill value is at most 10000 (a newly added player's skill value is at least 1). Find the minimum possible equal total; if it cannot be done, output −1.
Input
The first line contains two integers n and m; the second line contains n integers teamA; the third line contains m integers teamB. 1≤n,m≤100000, 0≤teamA[i],teamB[i]≤10000.
Output
Output one integer: the minimum equal total, or −1.
Example 1
Input:
4 5
5 10 0 4
2 4 0 5 0
Output:
20
Example 2
Input:
2 1
3 4
8
Output:
-1
Example 3
Input:
1 2
0
10000 1
Output:
-1
Example notes
Example 1: teamA=[5,10,0,4], teamB=[2,4,0,5,0]. Fill A with 1 and B with 3 and 6 to get [5,10,1,4] and [2,4,3,5,6]; both totals are 20, which is the minimum equal total. Example 2: neither team has an empty position, and the totals 7 and 8 are not equal, so output −1. Example 3: A has only one empty position, which can be filled with at most 10000, while B's total is 10001, so output −1.
Editorial
Loading solution…