#C6241. Minimum Traveling Distance for Package Delivery

    ID: 49980 Type: Default 1000ms 256MiB

Minimum Traveling Distance for Package Delivery

Minimum Traveling Distance for Package Delivery

You are given n packages that need to be delivered to various coordinates on a one-dimensional line. The delivery person starts at coordinate \(0\). The goal is to determine the minimum distance required to deliver all packages and return back to \(0\).

More formally, you are given an integer \(n\) and a list of \(n\) integers representing the delivery locations. Let \(L = \min(locations)\) and \(R = \max(locations)\). The minimum distance required is given by the formula:

[ \text{distance} = |L| + |R| + |R - L| ]

This formula effectively represents the sum of the distance to the leftmost point, the distance from the leftmost to the rightmost point, and the distance from the rightmost point back to the origin.

Note: If there are no packages (i.e. \(n = 0\)), the minimum distance is \(0\).

inputFormat

The input is read from standard input and consists of two lines:

  • The first line contains a single integer \(n\) representing the number of packages.
  • The second line contains \(n\) space-separated integers representing the delivery coordinates.

outputFormat

Output to standard output a single integer representing the minimum total distance that must be traveled.

## sample
3
-5 0 10
30