n = gets.to_i array = gets.split(" ").map(&:to_i).sort center = array[n/2] puts array.inject(0) {|sum, i| sum + (center-i).abs}