#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) void(23)
#endif
constexpr int max_N = int(2E6) + 5;
int N;
std::vector<int> adj[max_N];
int tp[max_N], siz[max_N];
std::pair<int, int> dfs(int v) {
debug(v, tp[v], adj[v]);
if (tp[v] == 0) {
return {0, 0};
}
int u0 = adj[v][0];
int u1 = adj[v][1];
auto r0 = dfs(u0);
auto r1 = dfs(u1);
siz[v] = siz[u0] + siz[u1];
std::pair<int, int> res;
if (tp[v] == -1) {
res = {std::min(r0.first, r1.first), r0.second + r1.second};
} else {
res = {r0.first + r1.first + 1, std::max(r0.second + siz[u1], r1.second + siz[u0])};
}
debug(v, res);
return res;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string S;
std::cin >> S;
int nv = 0;
std::vector<int> stk;
for (int i = 0; i < S.size(); ++i) {
if (S[i] == '(') {
tp[nv] = (S[i - 1] == 'n' ? -1 : +1);
if (!stk.empty()) {
adj[stk.back()].emplace_back(nv);
}
stk.emplace_back(nv++);
} else if (S[i] == '?') {
tp[nv] = 0;
siz[nv] = 1;
adj[stk.back()].emplace_back(nv++);
} else if (S[i] == ')') {
stk.pop_back();
}
}
N = nv;
auto x = dfs(0);
std::cout << x.second - x.first + 1 << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i = 0; i < S.size(); ++i) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50512 KB |
Output is correct |
2 |
Correct |
10 ms |
50512 KB |
Output is correct |
3 |
Correct |
10 ms |
50600 KB |
Output is correct |
4 |
Correct |
13 ms |
50512 KB |
Output is correct |
5 |
Correct |
10 ms |
50512 KB |
Output is correct |
6 |
Correct |
13 ms |
50512 KB |
Output is correct |
7 |
Correct |
12 ms |
50512 KB |
Output is correct |
8 |
Correct |
13 ms |
50524 KB |
Output is correct |
9 |
Correct |
10 ms |
50512 KB |
Output is correct |
10 |
Correct |
9 ms |
50684 KB |
Output is correct |
11 |
Correct |
10 ms |
50604 KB |
Output is correct |
12 |
Correct |
12 ms |
50512 KB |
Output is correct |
13 |
Correct |
11 ms |
50396 KB |
Output is correct |
14 |
Correct |
9 ms |
50384 KB |
Output is correct |
15 |
Correct |
11 ms |
50512 KB |
Output is correct |
16 |
Correct |
10 ms |
50512 KB |
Output is correct |
17 |
Correct |
10 ms |
50512 KB |
Output is correct |
18 |
Correct |
10 ms |
50512 KB |
Output is correct |
19 |
Correct |
10 ms |
50512 KB |
Output is correct |
20 |
Correct |
10 ms |
50512 KB |
Output is correct |
21 |
Correct |
10 ms |
50384 KB |
Output is correct |
22 |
Correct |
9 ms |
50512 KB |
Output is correct |
23 |
Correct |
12 ms |
50512 KB |
Output is correct |
24 |
Correct |
10 ms |
50512 KB |
Output is correct |
25 |
Correct |
9 ms |
50512 KB |
Output is correct |
26 |
Correct |
10 ms |
50512 KB |
Output is correct |
27 |
Correct |
10 ms |
50768 KB |
Output is correct |
28 |
Correct |
13 ms |
50512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50512 KB |
Output is correct |
2 |
Correct |
10 ms |
50512 KB |
Output is correct |
3 |
Correct |
10 ms |
50600 KB |
Output is correct |
4 |
Correct |
13 ms |
50512 KB |
Output is correct |
5 |
Correct |
10 ms |
50512 KB |
Output is correct |
6 |
Correct |
13 ms |
50512 KB |
Output is correct |
7 |
Correct |
12 ms |
50512 KB |
Output is correct |
8 |
Correct |
13 ms |
50524 KB |
Output is correct |
9 |
Correct |
10 ms |
50512 KB |
Output is correct |
10 |
Correct |
9 ms |
50684 KB |
Output is correct |
11 |
Correct |
10 ms |
50604 KB |
Output is correct |
12 |
Correct |
12 ms |
50512 KB |
Output is correct |
13 |
Correct |
11 ms |
50396 KB |
Output is correct |
14 |
Correct |
9 ms |
50384 KB |
Output is correct |
15 |
Correct |
11 ms |
50512 KB |
Output is correct |
16 |
Correct |
10 ms |
50512 KB |
Output is correct |
17 |
Correct |
10 ms |
50512 KB |
Output is correct |
18 |
Correct |
10 ms |
50512 KB |
Output is correct |
19 |
Correct |
10 ms |
50512 KB |
Output is correct |
20 |
Correct |
10 ms |
50512 KB |
Output is correct |
21 |
Correct |
10 ms |
50384 KB |
Output is correct |
22 |
Correct |
9 ms |
50512 KB |
Output is correct |
23 |
Correct |
12 ms |
50512 KB |
Output is correct |
24 |
Correct |
10 ms |
50512 KB |
Output is correct |
25 |
Correct |
9 ms |
50512 KB |
Output is correct |
26 |
Correct |
10 ms |
50512 KB |
Output is correct |
27 |
Correct |
10 ms |
50768 KB |
Output is correct |
28 |
Correct |
13 ms |
50512 KB |
Output is correct |
29 |
Correct |
9 ms |
50512 KB |
Output is correct |
30 |
Correct |
11 ms |
50512 KB |
Output is correct |
31 |
Correct |
10 ms |
50444 KB |
Output is correct |
32 |
Correct |
9 ms |
50512 KB |
Output is correct |
33 |
Correct |
10 ms |
50512 KB |
Output is correct |
34 |
Correct |
10 ms |
50368 KB |
Output is correct |
35 |
Correct |
10 ms |
50512 KB |
Output is correct |
36 |
Correct |
11 ms |
50512 KB |
Output is correct |
37 |
Correct |
10 ms |
50596 KB |
Output is correct |
38 |
Correct |
10 ms |
50512 KB |
Output is correct |
39 |
Correct |
10 ms |
50584 KB |
Output is correct |
40 |
Correct |
10 ms |
50512 KB |
Output is correct |
41 |
Correct |
10 ms |
50512 KB |
Output is correct |
42 |
Correct |
9 ms |
50512 KB |
Output is correct |
43 |
Correct |
10 ms |
50512 KB |
Output is correct |
44 |
Correct |
10 ms |
50512 KB |
Output is correct |
45 |
Correct |
10 ms |
50512 KB |
Output is correct |
46 |
Correct |
10 ms |
50512 KB |
Output is correct |
47 |
Correct |
13 ms |
50512 KB |
Output is correct |
48 |
Correct |
10 ms |
50512 KB |
Output is correct |
49 |
Correct |
9 ms |
50600 KB |
Output is correct |
50 |
Correct |
9 ms |
50512 KB |
Output is correct |
51 |
Correct |
10 ms |
50512 KB |
Output is correct |
52 |
Correct |
9 ms |
50512 KB |
Output is correct |
53 |
Correct |
10 ms |
50684 KB |
Output is correct |
54 |
Correct |
13 ms |
50512 KB |
Output is correct |
55 |
Correct |
10 ms |
50512 KB |
Output is correct |
56 |
Correct |
9 ms |
50512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
228 ms |
191752 KB |
Output is correct |
2 |
Correct |
222 ms |
190192 KB |
Output is correct |
3 |
Correct |
211 ms |
191696 KB |
Output is correct |
4 |
Correct |
226 ms |
191552 KB |
Output is correct |
5 |
Correct |
214 ms |
191692 KB |
Output is correct |
6 |
Correct |
219 ms |
191692 KB |
Output is correct |
7 |
Correct |
229 ms |
191740 KB |
Output is correct |
8 |
Correct |
244 ms |
191708 KB |
Output is correct |
9 |
Correct |
199 ms |
190128 KB |
Output is correct |
10 |
Correct |
201 ms |
191572 KB |
Output is correct |
11 |
Correct |
196 ms |
191636 KB |
Output is correct |
12 |
Correct |
198 ms |
191768 KB |
Output is correct |
13 |
Correct |
10 ms |
50616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50512 KB |
Output is correct |
2 |
Correct |
10 ms |
50512 KB |
Output is correct |
3 |
Correct |
10 ms |
50600 KB |
Output is correct |
4 |
Correct |
13 ms |
50512 KB |
Output is correct |
5 |
Correct |
10 ms |
50512 KB |
Output is correct |
6 |
Correct |
13 ms |
50512 KB |
Output is correct |
7 |
Correct |
12 ms |
50512 KB |
Output is correct |
8 |
Correct |
13 ms |
50524 KB |
Output is correct |
9 |
Correct |
10 ms |
50512 KB |
Output is correct |
10 |
Correct |
9 ms |
50684 KB |
Output is correct |
11 |
Correct |
10 ms |
50604 KB |
Output is correct |
12 |
Correct |
12 ms |
50512 KB |
Output is correct |
13 |
Correct |
11 ms |
50396 KB |
Output is correct |
14 |
Correct |
9 ms |
50384 KB |
Output is correct |
15 |
Correct |
11 ms |
50512 KB |
Output is correct |
16 |
Correct |
10 ms |
50512 KB |
Output is correct |
17 |
Correct |
10 ms |
50512 KB |
Output is correct |
18 |
Correct |
10 ms |
50512 KB |
Output is correct |
19 |
Correct |
10 ms |
50512 KB |
Output is correct |
20 |
Correct |
10 ms |
50512 KB |
Output is correct |
21 |
Correct |
10 ms |
50384 KB |
Output is correct |
22 |
Correct |
9 ms |
50512 KB |
Output is correct |
23 |
Correct |
12 ms |
50512 KB |
Output is correct |
24 |
Correct |
10 ms |
50512 KB |
Output is correct |
25 |
Correct |
9 ms |
50512 KB |
Output is correct |
26 |
Correct |
10 ms |
50512 KB |
Output is correct |
27 |
Correct |
10 ms |
50768 KB |
Output is correct |
28 |
Correct |
13 ms |
50512 KB |
Output is correct |
29 |
Correct |
9 ms |
50512 KB |
Output is correct |
30 |
Correct |
11 ms |
50512 KB |
Output is correct |
31 |
Correct |
10 ms |
50444 KB |
Output is correct |
32 |
Correct |
9 ms |
50512 KB |
Output is correct |
33 |
Correct |
10 ms |
50512 KB |
Output is correct |
34 |
Correct |
10 ms |
50368 KB |
Output is correct |
35 |
Correct |
10 ms |
50512 KB |
Output is correct |
36 |
Correct |
11 ms |
50512 KB |
Output is correct |
37 |
Correct |
10 ms |
50596 KB |
Output is correct |
38 |
Correct |
10 ms |
50512 KB |
Output is correct |
39 |
Correct |
10 ms |
50584 KB |
Output is correct |
40 |
Correct |
10 ms |
50512 KB |
Output is correct |
41 |
Correct |
10 ms |
50512 KB |
Output is correct |
42 |
Correct |
9 ms |
50512 KB |
Output is correct |
43 |
Correct |
10 ms |
50512 KB |
Output is correct |
44 |
Correct |
10 ms |
50512 KB |
Output is correct |
45 |
Correct |
10 ms |
50512 KB |
Output is correct |
46 |
Correct |
10 ms |
50512 KB |
Output is correct |
47 |
Correct |
13 ms |
50512 KB |
Output is correct |
48 |
Correct |
10 ms |
50512 KB |
Output is correct |
49 |
Correct |
9 ms |
50600 KB |
Output is correct |
50 |
Correct |
9 ms |
50512 KB |
Output is correct |
51 |
Correct |
10 ms |
50512 KB |
Output is correct |
52 |
Correct |
9 ms |
50512 KB |
Output is correct |
53 |
Correct |
10 ms |
50684 KB |
Output is correct |
54 |
Correct |
13 ms |
50512 KB |
Output is correct |
55 |
Correct |
10 ms |
50512 KB |
Output is correct |
56 |
Correct |
9 ms |
50512 KB |
Output is correct |
57 |
Correct |
9 ms |
50512 KB |
Output is correct |
58 |
Correct |
9 ms |
50624 KB |
Output is correct |
59 |
Correct |
9 ms |
50512 KB |
Output is correct |
60 |
Correct |
9 ms |
50512 KB |
Output is correct |
61 |
Correct |
9 ms |
50512 KB |
Output is correct |
62 |
Correct |
9 ms |
50512 KB |
Output is correct |
63 |
Correct |
9 ms |
50628 KB |
Output is correct |
64 |
Correct |
10 ms |
50512 KB |
Output is correct |
65 |
Correct |
11 ms |
50512 KB |
Output is correct |
66 |
Correct |
9 ms |
50512 KB |
Output is correct |
67 |
Correct |
9 ms |
50512 KB |
Output is correct |
68 |
Correct |
9 ms |
50416 KB |
Output is correct |
69 |
Correct |
9 ms |
50512 KB |
Output is correct |
70 |
Correct |
9 ms |
50512 KB |
Output is correct |
71 |
Correct |
9 ms |
50512 KB |
Output is correct |
72 |
Correct |
9 ms |
50684 KB |
Output is correct |
73 |
Correct |
9 ms |
50512 KB |
Output is correct |
74 |
Correct |
9 ms |
50680 KB |
Output is correct |
75 |
Correct |
9 ms |
50512 KB |
Output is correct |
76 |
Correct |
9 ms |
50512 KB |
Output is correct |
77 |
Correct |
9 ms |
50524 KB |
Output is correct |
78 |
Correct |
9 ms |
50660 KB |
Output is correct |
79 |
Correct |
9 ms |
50512 KB |
Output is correct |
80 |
Correct |
9 ms |
50512 KB |
Output is correct |
81 |
Correct |
10 ms |
50512 KB |
Output is correct |
82 |
Correct |
10 ms |
50512 KB |
Output is correct |
83 |
Correct |
11 ms |
50400 KB |
Output is correct |
84 |
Correct |
10 ms |
50512 KB |
Output is correct |
85 |
Correct |
9 ms |
50512 KB |
Output is correct |
86 |
Correct |
9 ms |
50388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50512 KB |
Output is correct |
2 |
Correct |
10 ms |
50512 KB |
Output is correct |
3 |
Correct |
10 ms |
50600 KB |
Output is correct |
4 |
Correct |
13 ms |
50512 KB |
Output is correct |
5 |
Correct |
10 ms |
50512 KB |
Output is correct |
6 |
Correct |
13 ms |
50512 KB |
Output is correct |
7 |
Correct |
12 ms |
50512 KB |
Output is correct |
8 |
Correct |
13 ms |
50524 KB |
Output is correct |
9 |
Correct |
10 ms |
50512 KB |
Output is correct |
10 |
Correct |
9 ms |
50684 KB |
Output is correct |
11 |
Correct |
10 ms |
50604 KB |
Output is correct |
12 |
Correct |
12 ms |
50512 KB |
Output is correct |
13 |
Correct |
11 ms |
50396 KB |
Output is correct |
14 |
Correct |
9 ms |
50384 KB |
Output is correct |
15 |
Correct |
11 ms |
50512 KB |
Output is correct |
16 |
Correct |
10 ms |
50512 KB |
Output is correct |
17 |
Correct |
10 ms |
50512 KB |
Output is correct |
18 |
Correct |
10 ms |
50512 KB |
Output is correct |
19 |
Correct |
10 ms |
50512 KB |
Output is correct |
20 |
Correct |
10 ms |
50512 KB |
Output is correct |
21 |
Correct |
10 ms |
50384 KB |
Output is correct |
22 |
Correct |
9 ms |
50512 KB |
Output is correct |
23 |
Correct |
12 ms |
50512 KB |
Output is correct |
24 |
Correct |
10 ms |
50512 KB |
Output is correct |
25 |
Correct |
9 ms |
50512 KB |
Output is correct |
26 |
Correct |
10 ms |
50512 KB |
Output is correct |
27 |
Correct |
10 ms |
50768 KB |
Output is correct |
28 |
Correct |
13 ms |
50512 KB |
Output is correct |
29 |
Correct |
9 ms |
50512 KB |
Output is correct |
30 |
Correct |
11 ms |
50512 KB |
Output is correct |
31 |
Correct |
10 ms |
50444 KB |
Output is correct |
32 |
Correct |
9 ms |
50512 KB |
Output is correct |
33 |
Correct |
10 ms |
50512 KB |
Output is correct |
34 |
Correct |
10 ms |
50368 KB |
Output is correct |
35 |
Correct |
10 ms |
50512 KB |
Output is correct |
36 |
Correct |
11 ms |
50512 KB |
Output is correct |
37 |
Correct |
10 ms |
50596 KB |
Output is correct |
38 |
Correct |
10 ms |
50512 KB |
Output is correct |
39 |
Correct |
10 ms |
50584 KB |
Output is correct |
40 |
Correct |
10 ms |
50512 KB |
Output is correct |
41 |
Correct |
10 ms |
50512 KB |
Output is correct |
42 |
Correct |
9 ms |
50512 KB |
Output is correct |
43 |
Correct |
10 ms |
50512 KB |
Output is correct |
44 |
Correct |
10 ms |
50512 KB |
Output is correct |
45 |
Correct |
10 ms |
50512 KB |
Output is correct |
46 |
Correct |
10 ms |
50512 KB |
Output is correct |
47 |
Correct |
13 ms |
50512 KB |
Output is correct |
48 |
Correct |
10 ms |
50512 KB |
Output is correct |
49 |
Correct |
9 ms |
50600 KB |
Output is correct |
50 |
Correct |
9 ms |
50512 KB |
Output is correct |
51 |
Correct |
10 ms |
50512 KB |
Output is correct |
52 |
Correct |
9 ms |
50512 KB |
Output is correct |
53 |
Correct |
10 ms |
50684 KB |
Output is correct |
54 |
Correct |
13 ms |
50512 KB |
Output is correct |
55 |
Correct |
10 ms |
50512 KB |
Output is correct |
56 |
Correct |
9 ms |
50512 KB |
Output is correct |
57 |
Correct |
228 ms |
191752 KB |
Output is correct |
58 |
Correct |
222 ms |
190192 KB |
Output is correct |
59 |
Correct |
211 ms |
191696 KB |
Output is correct |
60 |
Correct |
226 ms |
191552 KB |
Output is correct |
61 |
Correct |
214 ms |
191692 KB |
Output is correct |
62 |
Correct |
219 ms |
191692 KB |
Output is correct |
63 |
Correct |
229 ms |
191740 KB |
Output is correct |
64 |
Correct |
244 ms |
191708 KB |
Output is correct |
65 |
Correct |
199 ms |
190128 KB |
Output is correct |
66 |
Correct |
201 ms |
191572 KB |
Output is correct |
67 |
Correct |
196 ms |
191636 KB |
Output is correct |
68 |
Correct |
198 ms |
191768 KB |
Output is correct |
69 |
Correct |
10 ms |
50616 KB |
Output is correct |
70 |
Correct |
9 ms |
50512 KB |
Output is correct |
71 |
Correct |
9 ms |
50624 KB |
Output is correct |
72 |
Correct |
9 ms |
50512 KB |
Output is correct |
73 |
Correct |
9 ms |
50512 KB |
Output is correct |
74 |
Correct |
9 ms |
50512 KB |
Output is correct |
75 |
Correct |
9 ms |
50512 KB |
Output is correct |
76 |
Correct |
9 ms |
50628 KB |
Output is correct |
77 |
Correct |
10 ms |
50512 KB |
Output is correct |
78 |
Correct |
11 ms |
50512 KB |
Output is correct |
79 |
Correct |
9 ms |
50512 KB |
Output is correct |
80 |
Correct |
9 ms |
50512 KB |
Output is correct |
81 |
Correct |
9 ms |
50416 KB |
Output is correct |
82 |
Correct |
9 ms |
50512 KB |
Output is correct |
83 |
Correct |
9 ms |
50512 KB |
Output is correct |
84 |
Correct |
9 ms |
50512 KB |
Output is correct |
85 |
Correct |
9 ms |
50684 KB |
Output is correct |
86 |
Correct |
9 ms |
50512 KB |
Output is correct |
87 |
Correct |
9 ms |
50680 KB |
Output is correct |
88 |
Correct |
9 ms |
50512 KB |
Output is correct |
89 |
Correct |
9 ms |
50512 KB |
Output is correct |
90 |
Correct |
9 ms |
50524 KB |
Output is correct |
91 |
Correct |
9 ms |
50660 KB |
Output is correct |
92 |
Correct |
9 ms |
50512 KB |
Output is correct |
93 |
Correct |
9 ms |
50512 KB |
Output is correct |
94 |
Correct |
10 ms |
50512 KB |
Output is correct |
95 |
Correct |
10 ms |
50512 KB |
Output is correct |
96 |
Correct |
11 ms |
50400 KB |
Output is correct |
97 |
Correct |
10 ms |
50512 KB |
Output is correct |
98 |
Correct |
9 ms |
50512 KB |
Output is correct |
99 |
Correct |
9 ms |
50388 KB |
Output is correct |
100 |
Correct |
127 ms |
109208 KB |
Output is correct |
101 |
Correct |
125 ms |
109340 KB |
Output is correct |
102 |
Correct |
155 ms |
109260 KB |
Output is correct |
103 |
Correct |
120 ms |
109208 KB |
Output is correct |
104 |
Correct |
124 ms |
109332 KB |
Output is correct |
105 |
Correct |
171 ms |
136652 KB |
Output is correct |
106 |
Correct |
163 ms |
136652 KB |
Output is correct |
107 |
Correct |
156 ms |
136652 KB |
Output is correct |
108 |
Correct |
163 ms |
136724 KB |
Output is correct |
109 |
Correct |
168 ms |
136724 KB |
Output is correct |
110 |
Correct |
128 ms |
109428 KB |
Output is correct |
111 |
Correct |
149 ms |
109356 KB |
Output is correct |
112 |
Correct |
125 ms |
109516 KB |
Output is correct |
113 |
Correct |
127 ms |
109268 KB |
Output is correct |
114 |
Correct |
124 ms |
109272 KB |
Output is correct |
115 |
Correct |
229 ms |
191764 KB |
Output is correct |
116 |
Correct |
220 ms |
191696 KB |
Output is correct |
117 |
Correct |
215 ms |
191692 KB |
Output is correct |
118 |
Correct |
220 ms |
191692 KB |
Output is correct |
119 |
Correct |
234 ms |
191636 KB |
Output is correct |
120 |
Correct |
125 ms |
109516 KB |
Output is correct |
121 |
Correct |
118 ms |
109288 KB |
Output is correct |
122 |
Correct |
122 ms |
109204 KB |
Output is correct |
123 |
Correct |
133 ms |
109260 KB |
Output is correct |
124 |
Correct |
144 ms |
150296 KB |
Output is correct |
125 |
Correct |
146 ms |
150476 KB |
Output is correct |
126 |
Correct |
148 ms |
150520 KB |
Output is correct |
127 |
Correct |
149 ms |
150424 KB |
Output is correct |
128 |
Correct |
57 ms |
82888 KB |
Output is correct |
129 |
Correct |
58 ms |
82832 KB |
Output is correct |
130 |
Correct |
55 ms |
82940 KB |
Output is correct |
131 |
Correct |
9 ms |
50512 KB |
Output is correct |