#include using namespace std; struct parser{ int V = 0; vector> c; vector s; parser(string &S){ int p = 0; expr(S, p); } int number(string &S, int &p){ string tmp; if (S[p] == '-'){ tmp += S[p]; p++; } while (isdigit(S[p]) != 0){ tmp += S[p]; p++; } c.push_back(vector(0)); s.push_back(tmp); V++; return V - 1; } int factor(string &S, int &p){ if (isdigit(S[p]) != 0 || S[p] == '-'){ return number(S, p); } else { p++; int ans = expr(S, p); p++; return ans; } } int term(string &S, int &p){ int ans = factor(S, p); while (S[p] == '*'){ p++; int ans2 = factor(S, p); c.push_back(vector{ans, ans2}); s.push_back("*"); V++; ans = V - 1; } return ans; } int expr(string &S, int &p){ int ans = term(S, p); while (S[p] == '+' || S[p] == '-'){ char op = S[p]; p++; int ans2 = term(S, p); c.push_back(vector{ans, ans2}); s.push_back(string(1, op)); V++; ans = V - 1; } return ans; } }; int main(){ int N; cin >> N; string S; cin >> S; parser P(S); vector sz(P.V); for (int i = 0; i < P.V; i++){ if (P.c[i].empty()){ sz[i] = P.s[i].size(); } else { sz[i] = sz[P.c[i][0]] + sz[P.c[i][1]]; } } vector head(P.V, false); head[P.V - 1] = true; for (int i = 0; i < P.V; i++){ if (!P.c[i].empty()){ if (sz[P.c[i][0]] < sz[P.c[i][1]]){ swap(P.c[i][0], P.c[i][1]); if (P.s[i] == "-"){ P.s[i] = "--"; } } head[P.c[i][1]] = true; } } vector dp(P.V); for (int i = 0; i < P.V; i++){ if (head[i]){ vector id; for (int v = i; ; v = P.c[v][0]){ id.push_back(v); if (P.c[v].empty()){ break; } } dp[i] = stoll(P.s[id.back()]); id.pop_back(); if (!id.empty()){ int cnt = id.size(); vector> F(cnt); for (int j = 0; j < cnt; j++){ int v = id[j]; if (P.s[v] == "+"){ F[j] = make_pair(1, dp[P.c[v][1]]); } if (P.s[v] == "-"){ F[j] = make_pair(1, -dp[P.c[v][1]]); } if (P.s[v] == "--"){ F[j] = make_pair(-1, dp[P.c[v][1]]); } if (P.s[v] == "*"){ F[j] = make_pair(dp[P.c[v][1]], 0); } } auto dfs = [&](auto dfs, int L, int R) -> pair { if (R - L == 1){ return F[L]; } else { int m = (L + R) / 2; pair ansL = dfs(dfs, L, m); pair ansR = dfs(dfs, m, R); return make_pair(ansL.first * ansR.first, ansL.first * ansR.second + ansL.second); } }; pair ans = dfs(dfs, 0, cnt); dp[i] = dp[i] * ans.first + ans.second; } } } cout << dp[P.V - 1] << endl; }