CitadelOA #14
Math with Lego Blocks
Estimated MediumJudge-ready
Topics (AI-labelled)Greedy
Replace every 0 in the two arrays with an arbitrary positive integer, keeping the other values unchanged, so that the two totals are equal and as small as possible; output −1 if there is no solution. The filled values are not limited by the original arrays' upper bound of 10000.
Input
The first line contains n m, the second line contains rowA, and the third line contains rowB; 1≤n,m≤100000, original elements 0..10000.
Output
Output a single integer, the answer.
Example 1
Input:
2 1
0 2
3
Output:
3
Example 2
Input:
1 2
1
0 0
Output:
-1
Example 3
Input:
2 1
0 0
0
Output:
2
Example notes
The outputs of the three public examples are, in order: 3; -1; 2. These answers were verified by independent enumeration or direct simulation.
Editorial
Loading solution…