#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
#define pb push_back
#define fi first
#define se second
const int MAX = 2e5 + 5;
const int MOD = 1e9 + 7;
#define int ll
string s;
struct nosy{
	int l, r;
	int len;
};
int idx = 0;
nosy dfs(){
	if(s[idx] == '?'){
		idx++;
		return {1, 1, 1};	
	}
	
	int t = 0;
	idx++;
	
	if(s[idx] == 'a')t = 1;
	idx += 3;
	
	auto [l1, r1, len1] = dfs();
	idx++;
	auto [l2, r2, len2] = dfs();
	idx++;
	
	if(t == 0){
		return {min(l1, l2), r1 + r2 - 1, len1 + len2};
	}
	else return {l1 + l2, max(r1 + len2 , r2 + len1), len1 + len2};
}
signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> s;
	nosy ans = dfs();
	
	cout << ans.r - ans.l + 1 << endl;
	
	return 0;
}
| # | 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... |