def min(a,b); a < b ? a : b; end N = gets.to_i Y = gets.split.map(&:to_i).sort S = Array.new(N + 1, 0) Y.each_with_index do |y,i| S[i + 1] = S[i] + y end ans = N.times.inject(Float::INFINITY) do |a, m| l = m / 2 r = m + (N - m) / 2 y1 = Y[l] y2 = Y[r] s1 = l * y1 - (S[l] - S[0]) s2 = S[m] - S[l] - (m - l) * y1 s3 = (r - m) * y2 - (S[r] - S[m]) s4 = (S[N] - S[r]) - (N - r) * y2 min(a, s1 + s2 + s3 + s4) end puts Y[0] == Y[-1] ? 1 : ans