#K67217. Optimal Delivery Coordinate
Optimal Delivery Coordinate
Optimal Delivery Coordinate
You are given a one‐dimensional street with (n) houses located at various coordinates. Your task is to determine the optimal delivery coordinate and calculate the total Manhattan distance from this coordinate to all houses. The optimal delivery coordinate is defined as the median of the house coordinates. In cases where (n) is even, choose the smaller of the two middle values, i.e. if the sorted coordinates are (a_1, a_2, \dots, a_n), choose (a_{\frac{n}{2}}).
Formally, given coordinates (a_1, a_2, \dots, a_n) (after sorting), let (m =) (a_{\lceil \frac{n}{2} \rceil}) if (n) is odd, or (a_{\frac{n}{2}}) (with 1-indexing) if (n) is even. You must output (m) and the total distance (\displaystyle \sum_{i=1}^{n} |a_i - m|).
inputFormat
The input is given via standard input ((stdin)) and consists of two lines. The first line contains a single integer (n) denoting the number of houses. The second line contains (n) space-separated integers representing the coordinates of the houses.
outputFormat
Output via standard output ((stdout)) two space-separated integers: the optimal coordinate for delivery, and the total Manhattan distance from this coordinate to all houses.## sample
1
5
5 0