if !ENV['RUBY_THREAD_VM_STACK_SIZE'] exec({'RUBY_THREAD_VM_STACK_SIZE'=>'1000000000'}, '/usr/bin/ruby', $0) end 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 ?* @i += 1 case @s[@i] when /\d/ t[1] *= @s[@i].to_i else t[0] += 1 end @i += 1 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*' '