#C7631. Equal Sum by Swapping Coins
Equal Sum by Swapping Coins
Equal Sum by Swapping Coins
In this problem, you are given two sets of coins, each with a positive integer value. You can swap exactly one coin from the first set with one from the second set. Determine if it is possible to equalize the total sums of the two sets by performing such a swap. Mathematically, let (S_1) and (S_2) be the sums of the first and second set respectively. For a valid swap, there must exist coins (a) from the first set and (b) from the second set such that:
[ S_1 - a + b = S_2 - b + a ]
which simplifies to
[ 2(b - a) = S_2 - S_1 ]
If such a pair exists, output YES
; otherwise, output NO
.
inputFormat
The input is read from standard input (stdin) and consists of three lines:
- The first line contains two integers (n) and (m), representing the number of coins in the first and second set respectively.
- The second line contains (n) space-separated integers, representing the values of the coins in the first set.
- The third line contains (m) space-separated integers, representing the values of the coins in the second set.
outputFormat
Output a single line to standard output (stdout) containing YES
if it is possible to equalize the sums by swapping one coin from each set; otherwise, output NO
.## sample
3 3
1 2 5
2 4 6
YES