Table of Contents

SolutionImplementation

Solution

Unofficial Editorial

Another solution and some proofs

Implementation

Time Complexity: O(NlogN)\mathcal{O}(N \log N)

n = int(input())
sticks = sorted(list(map(int, input().split())))
median = sticks[n // 2]
ans = 0
for 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!