n = gets.to_i ys = gets.chomp.split.map(&:to_i) left = ys.min right = ys.max 100.times do r1 = (left + left + right) / 3 r2 = (left + right + right) / 3 s1 = ys.map { |y| (y - r1).abs }.inject :+ s2 = ys.map { |y| (y - r2).abs }.inject :+ if s1 < s2 right = r2 else left = r1 end end ans = 100000000000 (left..right).each do |r| s = ys.map { |y| (y - r).abs }.inject :+ ans = [ans, s].min end p ans