In this problem, we are given an array with elements. We want to find the minimum cost to make all of the elements equal.
Solution
Another solution and some proofs
Implementation
Here is the code implementation for the easiest approach.
n = int(input())sticks = sorted(list(map(int, input().split())))median = sticks[n // 2]ans = 0for x in sticks:ans += abs(median - x)print(ans)
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!