AmazonOA #293
Minimize Sum of Absolute Differences
Estimated EasyJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Use every element of two equal-length arrays exactly once to form n pairs. The same position in b cannot be matched by different positions in a, and different positions holding the same value still count as separate elements. Minimize the total of the absolute differences within each pair.
Input
The first line contains n, the second line contains a, and the third line contains b. The main raw constraints explicitly say Unknown; this site adds 1≤n≤100000, −10^9≤a[i],b[i]≤10^9. This range does not pretend to be the source's original bounds.
Output
Output a single integer, the answer.
Example 1
Input:
2
1 10
9 2
Output:
2
Example 2
Input:
2
-5 2
4 -3
Output:
4
Example 3
Input:
2
3 3
3 3
Output:
0
Example notes
The answers are 2, 4 and 0 respectively. In the second example, pair −5 with −3 and 2 with 4; each pair differs by 2.
Editorial
Loading solution…