Make heights uniform

In an effort to optimize costs, the city design department has reached out to you to help them make all the buildings have the same height. All the buildings having uniform height will drastically reduce construction costs.
They have prepared n buildings in the city that have different heights . Changing the height of a building by x, will result in a meeting that will last x minutes. As you’re busy, you’d like to minimize the time spent in meetings. What would be the minimal possible time spent in meetings?


The first line of the input contains a single integer n (1 ≀ n ≀ ).
The next line contains n space-separated integers (1 ≀ ≀ ) the initial heights of the buildings.


The program should print a single integer - the minimal time spent in meetings.


5 1 4 8 2 9


The optimal height is 4 β‡’ we need to change 1 β†’ 4 (3-minute meeting), 2 β†’ 4 (2-minute meeting), 8 β†’ 4 (4-minute meeting), 9 β†’ 4 (5-minute meeting) β‡’ 14 minutes in total.


Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue