#C3649. Optimize Your Route to Minimize Travel Distance
Optimize Your Route to Minimize Travel Distance
Optimize Your Route to Minimize Travel Distance
Given an integer n and a list of n integers representing the coordinates of houses located on a one-dimensional line, determine the minimum total travel distance required to visit all houses starting from and returning to the origin (0). The optimal visiting order is achieved by sorting the coordinates in non-decreasing order.
The total travel distance, \(D\), is calculated as:
$$D = |x_1| + \sum_{i=2}^{n}(x_i - x_{i-1}) + |x_n|$$where \(x_1, x_2, \ldots, x_n\) are the sorted coordinates.
inputFormat
The first line contains an integer n, representing the number of houses. The second line contains n space-separated integers representing the coordinates of these houses.
outputFormat
Output two lines. The first line should contain the minimum total travel distance. The second line should contain the optimal visiting order (the n house coordinates sorted in non-decreasing order) separated by spaces.## sample
1
10
20
10
</p>