#K61842. Minimizing Total Travel Distance
Minimizing Total Travel Distance
Minimizing Total Travel Distance
You are given n parked cars on a long straight road with their positions provided in an array. Your task is to determine the minimized total travel distance for a new car when parked at an optimal position. Mathematically, if the positions are \(x_1, x_2, \dots, x_n\), you need to find the optimal parking spot \(m\) such that the sum \(\sum_{i=1}^{n} |x_i - m|\) is minimized.
Hint: The optimal solution is achieved by parking at the median of the positions.
inputFormat
The input consists of two lines:
- The first line contains an integer n representing the number of parked cars.
- The second line contains n space-separated integers, each indicating the position of a parked car.
outputFormat
Output a single integer that represents the minimized total travel distance when the new car is parked at the optimal position.
## sample5
1 2 3 6 7
10