#K39987. Minimum Wood Required
Minimum Wood Required
Minimum Wood Required
You are given n houses along a one-dimensional coordinate line. The houses can be located at any integer positions including negative values. Your task is to compute the minimum amount of wood required to connect all houses in a single path. The wood required to connect two houses is equal to the absolute difference of their positions.
If the positions of the houses are sorted as \(x_1 \le x_2 \le \ldots \le x_n\), then the minimum wood required is:
\[ \text{wood} = x_n - x_1 \]This works because connecting the houses sequentially from the smallest position to the largest minimizes unnecessary extra length.
inputFormat
The input is given via standard input.
The first line contains an integer n (the number of houses).
The second line contains n space-separated integers representing the positions of the houses on the coordinate line.
outputFormat
Output a single integer, which is the minimum amount of wood required to connect all the houses, printed to standard output.
## sample4
1 3 4 6
5