#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#define int long long
#define pii pair<int,int>
#define vi vector<int>
#define ff first
#define ss second
#define sp << " " <<
#define all(x) x.begin(),x.end()
#define big(x) ((int)(x.size()))
using namespace std;
const int MOD = 1e9+7, LIM = 1e6+1, inf = 2e9;
const int N = 1e5+1;
struct Node {
int n,l,r;
};
Node mn(Node p1,Node p2) {
return {p1.n+p2.n,min(p1.l,p2.l),p1.r+p2.r-1};
}
Node mx(Node p1,Node p2) {
return {p1.n+p2.n,p1.l+p2.l,max(p1.n+p2.r,p2.n+p1.r)};
}
string s;
Node f(int l,int r) {
if (l == r) return {1,1,1};
int ptr = l+4;
int bal = 0;
int bal2 = 0;
int ptr2 = r-1;
int h = 0;
for (;ptr<=r && ptr2 >= l;ptr++,ptr2--) {
if (s[ptr] == '(') bal++;
if (s[ptr] == ')') bal--;
if (s[ptr2] == ')') bal2++;
if (s[ptr2] == '(') bal2--;
if (!bal && s[ptr] == ',') {
h = 1;
break;
}
if (!bal2 && s[ptr2] == ',') {
h = 2;
break;
}
}
assert(h);
Node e1,e2;
if (h == 1) e1 = f(l+4,ptr-1),e2 = f(ptr+1,r-1);
else e1 = f(l+4,ptr2-1),e2 = f(ptr2+1,r-1);
if (s.substr(l,3) == "max") return mx(e1,e2);
return mn(e1,e2);
}
void solve() {
string tree;
cin >> tree;
s = tree;
Node p = f(0,tree.size()-1);
cout << p.r-p.l+1 << '\n';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef Dodi
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |