#include using namespace std; int main() { while(true) { int N; cin >> N; if(cin.eof()){ return 0; } string newString = ""; char nextChar; char prevChar; cin >> prevChar; // create new string - no need for online for(int i = 1; i < N; i++){ newString += prevChar; cin >> nextChar; if(prevChar == '('){ if(nextChar == ')'){ newString += "1"; } } else { if(nextChar == ')'){ newString += "+1"; } else { newString += "*"; } } prevChar = nextChar; } newString += prevChar; stack ops; stack values; unordered_map PRIORITY; PRIORITY['('] = 1; PRIORITY[')'] = 2; PRIORITY['+'] = 3; // 10 ((())(())) PRIORITY['*'] = 4; int oneCount = 0; for(const auto &c: newString){ if(c == '1'){ oneCount++; } } for(const auto &c : newString){ if(c != '1'){ while(c != '(' && !ops.empty() && PRIORITY[ops.top()] >= PRIORITY[c]){ auto right = values.top(); values.pop(); auto left = values.top(); values.pop(); if(ops.top() == '+'){ values.push(left + right); } else { values.push(left * right); } ops.pop(); } if(c == ')'){ ops.pop(); } else { ops.push(c); } } else { values.push(1); } } while(!ops.empty()){ if(ops.top() == '(' || ops.top() == ')'){ ops.pop(); continue; } auto right = values.top(); values.pop(); auto left = values.top(); values.pop(); if(ops.top() == '+'){ values.push(left + right); } else { values.push(left * right); } ops.pop(); } cout << values.top() << endl; } return 0; }