You are given two arrays:
a[i] = number of candies friend i has
b[i] = number of oranges friend i has
In one move, you may reduce:
-
a[i] by 1
-
b[i] by 1
(or both together)
Your goal is to make all a[i] equal to the minimum value in array a,
and all b[i] equal to the minimum value in array b,
using the minimum possible number of moves.
Every friend i needs:
-
(a[i] - min(a)) deletions for candies
-
(b[i] - min(b)) deletions for oranges
But because the operation can reduce both at the same time,
the number of moves for friend i is:
max(a[i] - min(a), b[i] - min(b))
Sum this over all friends.
n = number of friends
array a of size n
array b of size n
Minimum number of moves required.
Example
Input:
3
1 2 3
2 1 2
Output:
2
Explanation:
min(a) = 1
min(b) = 1
For each friend:
Friend 1: already at minimum → 0 moves
Friend 2: reduce a by 1 and b by 0 → 1 move
Friend 3: reduce a by 2 and b by 1 → max(2,1) = 2 moves
Total = 0 + 1 + 2 = 3
(Example outputs may vary based on actual CF test).
The formula holds for all cases.
-
1 ≤ n ≤ 50
-
1 ≤ a[i], b[i] ≤ 10^9
-
Find global minima of a and b.
-
For each index, compute required candy and orange reductions.
-
Moves needed = max(candy_diff, orange_diff).
-
Add for all i.
Since reducing both values in one move is allowed,
you always pair reductions whenever possible.
Only the larger of the two deficits determines total moves for that person.