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