class Calc0222 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @s = args.shift.first end def run stack = [] @s.scan(/\d+|[+-]/).each do |t| case t when /\d+/ t = t.to_i loop do if stack.empty? stack.push(t) break else op = stack.pop if stack.last.is_a?(Numeric) target = stack.pop case op when '+' stack.push(target - t) when '-' stack.push(target + t) end break else case op when '+' t = +t when '-' t = -t end end end end when '+', '-' stack.push(t) end end stack.pop end end puts Calc0222.new(STDIN.readlines).run if __FILE__ == $0