AmazonOA #36
Amazon Security Analysis (Min Distance Matching)
Estimated EasyJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Match n centers with n destinations one-to-one; each pair costs the absolute difference of their coordinates. Find the minimum total cost.
Input
The first line contains n (1..100000), the second line contains n center coordinates, and the third line contains n destination coordinates, all in 1..10⁹.
Output
Output a single integer, the answer.
Example 1
Input:
3
1 2 2
5 2 4
Output:
6
Example 2
Input:
1
1
9
Output:
8
Example 3
Input:
2
4 1
1 4
Output:
0
Example notes
Example 1: after sorting, pair 1→2, 2→4 and 2→5, for a distance of 1+2+3=6. Example 2: the only distance is 8. Example 3: identical coordinates are paired with each other, costing 0.
Editorial
Loading solution…