cswork / OA打开练习工作台

MathWorksOA #20

Balancing Teams

简单可在线判题

考点(AI 标注)贪心调度原地数组

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.

输入格式

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 one integer: the minimum equal total, or −1.

样例 1

输入:

4 5
5 10 0 4
2 4 0 5 0

输出:

20

样例 2

输入:

2 1
3 4
8

输出:

-1

样例 3

输入:

1 2
0
10000 1

输出:

-1

样例说明

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.