import std.stdio; import std.string; import std.conv; import std.bigint; import std.container.slist; string[] lex(string s){ string[] result; long i = 0; while(i < s.length){ switch(s[i]){ case '+', '-', '*', '(', ')': result ~= s[i .. i + 1]; i++; break; default: auto j = i; while(i < s.length && s[i] >= '0' && s[i] <= '9'){ i++; } result ~= s[j .. i]; } } return result; } int[string] priority = ["+" : 2, "-" : 2, "*" : 1, "(" : 3]; string[] rpn(string[] l){ string[] result; auto stack = SList!string(); auto minus = false; foreach(i, s; l){ auto p = priority.get(s, 0); switch(s){ case ")": while(!stack.empty && stack.front != "("){ result ~= stack.front; stack.removeFront; } stack.removeFront; break; case "(": stack.insert(s); break; case "-": if(stack.empty || l[i - 1] in priority){ minus = !minus; break; } goto default; default: while(!stack.empty && p >= priority.get(stack.front, 0)){ result ~= stack.front; stack.removeFront; } if(minus){ s = "-" ~ s; } minus = false; stack.insert(s); } } while(!stack.empty){ result ~= stack.front; stack.removeFront; } return result; } BigInt eval(string[] l){ auto stack = SList!BigInt(); foreach(s; l){ switch(s){ case "+": auto a = stack.front; stack.removeFront; stack.front += a; break; case "-": auto a = stack.front; stack.removeFront; stack.front -= a; break; case "*": auto a = stack.front; stack.removeFront; stack.front *= a; break; default: stack.insert(BigInt(s)); } } return stack.front; } void main(){ auto N = readln.chomp.to!long; auto S = readln.chomp; writeln(S.lex.rpn.eval); }