Problem 972
Minimum Number of Moves to Seat Everyone
Easy · 10 pts · Array, Greedy, Sorting
Problem
There are N seats and N students in a row, at the given integer positions (several may share a position). In one move a student steps one position left or right. Seat every student in a different seat with the fewest total moves. Sorting both lists and matching the i-th smallest student with the i-th smallest seat is optimal, so the answer is the sum of |SEATS[i] - STUDENTS[i]| after sorting.
Input
The first line contains N. The second line contains the seat positions. The third line contains the student positions.
Output
Print the minimum total number of moves.
Constraints
1 <= N <= 100; 1 <= positions <= 100
Example input
3 3 1 5 2 7 4
Example output
4
Explanation
With seats 3 1 5 and students 2 7 4, sorted they are 1 3 5 and 2 4 7. Matching in order costs 1 + 1 + 2 = 4 moves.