#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 ((())(())) 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(); ops.pop(); values.push(left + right); } if(c == ')'){ ops.pop(); } else { ops.push(c); } } else { values.push(1); } } cout << values.top() << endl; } return 0; }