#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define int long long
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int x, y; cin >> x >> y, x--, y--;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
vector<int> a(n);
for (int i = 0; i < n; i++) {
char x; cin >> x;
a[i] = x - '0';
}
vector<int> dp(n);
int ans = 0;
function<void(int, int)> dfs = [&](int node, int parent) {
dp[node] = -a[node];
for (auto next : g[node]) {
if (next == parent) continue;
dfs(next, node);
ans = max(ans, dp[next] + a[node]);
dp[node] += dp[next];
}
dp[node] = max(dp[node], a[node]);
ans = max(ans, dp[node]);
};
dfs(0, -1);
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
452 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
460 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
460 KB |
Output is correct |
44 |
Correct |
1 ms |
460 KB |
Output is correct |
45 |
Correct |
1 ms |
472 KB |
Output is correct |
46 |
Correct |
1 ms |
460 KB |
Output is correct |
47 |
Correct |
2 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
588 KB |
Output is correct |
49 |
Correct |
2 ms |
588 KB |
Output is correct |
50 |
Correct |
1 ms |
460 KB |
Output is correct |
51 |
Correct |
1 ms |
460 KB |
Output is correct |
52 |
Correct |
1 ms |
460 KB |
Output is correct |
53 |
Correct |
1 ms |
460 KB |
Output is correct |
54 |
Correct |
1 ms |
460 KB |
Output is correct |
55 |
Correct |
1 ms |
460 KB |
Output is correct |
56 |
Correct |
1 ms |
456 KB |
Output is correct |
57 |
Correct |
1 ms |
588 KB |
Output is correct |
58 |
Correct |
1 ms |
576 KB |
Output is correct |
59 |
Correct |
1 ms |
460 KB |
Output is correct |
60 |
Correct |
1 ms |
576 KB |
Output is correct |
61 |
Correct |
1 ms |
460 KB |
Output is correct |
62 |
Correct |
1 ms |
580 KB |
Output is correct |
63 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
452 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
460 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
460 KB |
Output is correct |
44 |
Correct |
1 ms |
460 KB |
Output is correct |
45 |
Correct |
1 ms |
472 KB |
Output is correct |
46 |
Correct |
1 ms |
460 KB |
Output is correct |
47 |
Correct |
2 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
588 KB |
Output is correct |
49 |
Correct |
2 ms |
588 KB |
Output is correct |
50 |
Correct |
1 ms |
460 KB |
Output is correct |
51 |
Correct |
1 ms |
460 KB |
Output is correct |
52 |
Correct |
1 ms |
460 KB |
Output is correct |
53 |
Correct |
1 ms |
460 KB |
Output is correct |
54 |
Correct |
1 ms |
460 KB |
Output is correct |
55 |
Correct |
1 ms |
460 KB |
Output is correct |
56 |
Correct |
1 ms |
456 KB |
Output is correct |
57 |
Correct |
1 ms |
588 KB |
Output is correct |
58 |
Correct |
1 ms |
576 KB |
Output is correct |
59 |
Correct |
1 ms |
460 KB |
Output is correct |
60 |
Correct |
1 ms |
576 KB |
Output is correct |
61 |
Correct |
1 ms |
460 KB |
Output is correct |
62 |
Correct |
1 ms |
580 KB |
Output is correct |
63 |
Correct |
1 ms |
460 KB |
Output is correct |
64 |
Correct |
188 ms |
18156 KB |
Output is correct |
65 |
Correct |
159 ms |
18116 KB |
Output is correct |
66 |
Correct |
169 ms |
18172 KB |
Output is correct |
67 |
Correct |
201 ms |
18208 KB |
Output is correct |
68 |
Correct |
196 ms |
18212 KB |
Output is correct |
69 |
Correct |
170 ms |
18116 KB |
Output is correct |
70 |
Correct |
164 ms |
18096 KB |
Output is correct |
71 |
Correct |
206 ms |
18152 KB |
Output is correct |
72 |
Correct |
195 ms |
18104 KB |
Output is correct |
73 |
Correct |
199 ms |
34924 KB |
Output is correct |
74 |
Correct |
191 ms |
34804 KB |
Output is correct |
75 |
Correct |
178 ms |
19356 KB |
Output is correct |
76 |
Correct |
162 ms |
19400 KB |
Output is correct |
77 |
Correct |
173 ms |
18112 KB |
Output is correct |
78 |
Correct |
159 ms |
18168 KB |
Output is correct |
79 |
Correct |
193 ms |
18076 KB |
Output is correct |
80 |
Correct |
167 ms |
18140 KB |
Output is correct |
81 |
Correct |
173 ms |
18220 KB |
Output is correct |
82 |
Correct |
180 ms |
18112 KB |
Output is correct |
83 |
Correct |
167 ms |
25276 KB |
Output is correct |
84 |
Correct |
196 ms |
28240 KB |
Output is correct |
85 |
Correct |
196 ms |
27884 KB |
Output is correct |
86 |
Correct |
161 ms |
20144 KB |
Output is correct |
87 |
Correct |
170 ms |
19264 KB |
Output is correct |
88 |
Correct |
148 ms |
19304 KB |
Output is correct |
89 |
Correct |
148 ms |
19256 KB |
Output is correct |
90 |
Correct |
147 ms |
19112 KB |
Output is correct |
91 |
Correct |
174 ms |
19356 KB |
Output is correct |
92 |
Correct |
155 ms |
19784 KB |
Output is correct |
93 |
Correct |
133 ms |
23020 KB |
Output is correct |
94 |
Correct |
171 ms |
25396 KB |
Output is correct |
95 |
Correct |
157 ms |
23600 KB |
Output is correct |
96 |
Correct |
176 ms |
21148 KB |
Output is correct |
97 |
Correct |
156 ms |
19052 KB |
Output is correct |
98 |
Correct |
140 ms |
18372 KB |
Output is correct |
99 |
Correct |
143 ms |
18268 KB |
Output is correct |
100 |
Correct |
187 ms |
22368 KB |
Output is correct |
101 |
Correct |
159 ms |
19512 KB |
Output is correct |
102 |
Correct |
147 ms |
18580 KB |
Output is correct |
103 |
Correct |
147 ms |
18452 KB |
Output is correct |
104 |
Correct |
110 ms |
18844 KB |
Output is correct |
105 |
Correct |
199 ms |
30352 KB |
Output is correct |
106 |
Correct |
107 ms |
18536 KB |
Output is correct |
107 |
Correct |
110 ms |
18496 KB |
Output is correct |
108 |
Correct |
199 ms |
35908 KB |
Output is correct |
109 |
Correct |
96 ms |
18736 KB |
Output is correct |
110 |
Correct |
177 ms |
36420 KB |
Output is correct |
111 |
Correct |
141 ms |
18116 KB |
Output is correct |