class Calc0198 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @b = args.shift.first.to_i @n = args.shift.first.to_i @cs = args.map(&:first).map(&:to_i) end def run med = [@cs.sort[@n / 2], (@cs.inject(:+) + @b) / @n].min @cs.map { |c| (med - c).abs }.inject(:+) end end puts Calc0198.new(STDIN.readlines).run if __FILE__ == $0