N = gets.to_i d = gets.to_i S = gets.chomp MEMO = Hash.new stack = [] S.chars.each_with_index do |s, idx| if s == '{' stack << idx elsif s == '}' r_idx = stack.pop MEMO[idx] = r_idx MEMO[r_idx] = idx end end def parse(line, left, right) i = left a = 0 d = 0 res = Hash.new(0) while i <= right ch = line[i] case ch when 'd' l = i + 1 r = MEMO[l] ret = parse(line, l + 1, r - 1) ret.each do |deg, e| next if deg == 0 res[deg - 1] += e * deg end i = r + 1 when 'x' d += 1 a = 1 if a == 0 i += 1 when '*' i += 1 when '+' res[d] += a d = 0 a = 0 i += 1 when '{' i += 1 else if d == 0 a += ch.to_i else a *= ch.to_i end i += 1 end end if a != 0 || d != 0 res[d] += a end res end res = parse(S, 0, N - 1) puts [*0..d].map { |i| res[i] }.join(' ')