gets @m = gets.to_i @s = gets.chomp @i = 0 @s << ?} def calc ret = [0]*(@m+1) t = [0, 0] while true do case @s[@i] when /\d/ t = [0, @s[@i].to_i] @i += 1 when ?* t[0] += 1 @i += 2 when ?x t = [1, 1] @i += 1 when ?+ ret[t[0]] += t[1] t = [0, 0] @i += 1 when ?d @i += 2 _t = calc (1..@m).each do |j| ret[j-1] += j * _t[j] end when ?} ret[t[0]] += t[1] @i += 1 return ret else end end end puts calc*' '