#P10452. 货仓选址

货仓选址

Problem Description

There are NN stores on a number line, with coordinates A1ANA_1 \sim A_N.

Now you need to build a warehouse on the number line. Every morning, one truck of goods must be delivered from the warehouse to each store.

To improve efficiency, find where to build the warehouse so that the sum of distances from the warehouse to all stores is minimized.

Input Format

The first line contains an integer NN.

The second line contains NN integers A1ANA_1 \sim A_N.

Output Format

Output one integer, representing the minimum possible sum of distances.

4
6 2 9 1
12

Hint

Constraints: 1N1051 \le N \le 10^5, 0Ai400000 \le A_i \le 40000.

Translated by ChatGPT 5