class Yukicoder def initialize b = gets.to_i n = gets.to_i c = [] n.times do c << gets.to_i end answer = 0 loop do c = c.sort.reverse a = c.inject(:+)/2 max_val = c.max b = c.inject(0){|sum, n| sum += (max_val-n)} if a < b d = c.find{|i| i < max_val} c[0] = d answer += (max_val-d) else answer += b break end end puts answer end end Yukicoder.new