#P12165. Minimizing Cable Consumption
Minimizing Cable Consumption
Minimizing Cable Consumption
On a one-dimensional line, there are n monitors and n power outlets. A teacher has given a task: each monitor must be connected to a unique outlet using a power cable. In doing so, the total length of the power cables used should be minimized. The distance between any two points is the absolute difference of their coordinates.
To facilitate computation, you only need to consider the straight line distance. The optimal strategy is to sort the positions of the monitors and the outlets and then connect the corresponding pairs. Mathematically, if the monitors' positions after sorting are \(x_1, x_2, \ldots, x_n\) and the outlets' positions after sorting are \(y_1, y_2, \ldots, y_n\), the minimal total cable length is given by:
$$\sum_{i=1}^{n} |x_i - y_i|$$
inputFormat
The first line contains an integer n (1 ≤ n ≤ 105), representing the number of monitors (and outlets).
The second line contains n integers, representing the positions of the monitors.
The third line contains n integers, representing the positions of the power outlets.
outputFormat
Output a single integer: the minimal total length of the power cables required.
sample
3
1 2 3
2 3 4
3