#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define debug if(0)
/*
ans: dp[1][0]
dp[v][1] = max(a[v], sum(dp[u][1])-a[v]), for u := child of v
dp[v][0] = max(sum(dp[u][1])-a[v], max(dp[u][1])+a[v], max(dp[u][0]))
*/
const int N = 2e5 + 4;
vector<int> V[N];
int dp[N][2], a[N];
void dfs(int v, int p=0) {
int max0 = 0, max1 = 0, sum = 0;
for(int u : V[v]) {
if(u != p) {
dfs(u, v);
max0 = max(max0, dp[u][0]);
max1 = max(max1, dp[u][1]);
sum += dp[u][1];
}
}
dp[v][0] = max(sum-a[v], max(max1+a[v], max0));
dp[v][1] = max(a[v], sum-a[v]);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=0; i<(n-1); i++) {
int a, b; cin>>a>>b;
V[a].push_back(b);
V[b].push_back(a);
}
string s; cin>>s;
for(int i=1; i<=n; i++) a[i] = (s[i-1] - '0');
dfs(1);
cout<<dp[1][0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5024 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5020 KB |
Output is correct |
13 |
Correct |
3 ms |
5020 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5024 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5020 KB |
Output is correct |
13 |
Correct |
3 ms |
5020 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5040 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5036 KB |
Output is correct |
27 |
Correct |
4 ms |
5036 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5220 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5032 KB |
Output is correct |
33 |
Correct |
3 ms |
5032 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5036 KB |
Output is correct |
37 |
Correct |
3 ms |
5064 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5204 KB |
Output is correct |
40 |
Correct |
3 ms |
5160 KB |
Output is correct |
41 |
Correct |
3 ms |
5164 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
4 ms |
5036 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5076 KB |
Output is correct |
51 |
Correct |
4 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5036 KB |
Output is correct |
53 |
Correct |
4 ms |
5164 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5292 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
3 ms |
5080 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5024 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5020 KB |
Output is correct |
13 |
Correct |
3 ms |
5020 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5040 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5036 KB |
Output is correct |
27 |
Correct |
4 ms |
5036 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5220 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5032 KB |
Output is correct |
33 |
Correct |
3 ms |
5032 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5036 KB |
Output is correct |
37 |
Correct |
3 ms |
5064 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
4 ms |
5204 KB |
Output is correct |
40 |
Correct |
3 ms |
5160 KB |
Output is correct |
41 |
Correct |
3 ms |
5164 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
4 ms |
5036 KB |
Output is correct |
44 |
Correct |
4 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5076 KB |
Output is correct |
51 |
Correct |
4 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5036 KB |
Output is correct |
53 |
Correct |
4 ms |
5164 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5292 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
3 ms |
5080 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
64 |
Correct |
139 ms |
16748 KB |
Output is correct |
65 |
Correct |
133 ms |
16768 KB |
Output is correct |
66 |
Correct |
144 ms |
16736 KB |
Output is correct |
67 |
Correct |
143 ms |
16764 KB |
Output is correct |
68 |
Correct |
135 ms |
16720 KB |
Output is correct |
69 |
Correct |
132 ms |
16780 KB |
Output is correct |
70 |
Correct |
125 ms |
16772 KB |
Output is correct |
71 |
Correct |
148 ms |
16672 KB |
Output is correct |
72 |
Correct |
116 ms |
16732 KB |
Output is correct |
73 |
Correct |
145 ms |
31828 KB |
Output is correct |
74 |
Correct |
165 ms |
31952 KB |
Output is correct |
75 |
Correct |
117 ms |
16652 KB |
Output is correct |
76 |
Correct |
117 ms |
16616 KB |
Output is correct |
77 |
Correct |
139 ms |
16576 KB |
Output is correct |
78 |
Correct |
139 ms |
16632 KB |
Output is correct |
79 |
Correct |
121 ms |
16648 KB |
Output is correct |
80 |
Correct |
121 ms |
16648 KB |
Output is correct |
81 |
Correct |
113 ms |
17056 KB |
Output is correct |
82 |
Correct |
105 ms |
16836 KB |
Output is correct |
83 |
Correct |
126 ms |
22896 KB |
Output is correct |
84 |
Correct |
148 ms |
25444 KB |
Output is correct |
85 |
Correct |
138 ms |
24984 KB |
Output is correct |
86 |
Correct |
125 ms |
18316 KB |
Output is correct |
87 |
Correct |
132 ms |
17648 KB |
Output is correct |
88 |
Correct |
140 ms |
18088 KB |
Output is correct |
89 |
Correct |
124 ms |
18288 KB |
Output is correct |
90 |
Correct |
116 ms |
17892 KB |
Output is correct |
91 |
Correct |
133 ms |
18160 KB |
Output is correct |
92 |
Correct |
109 ms |
18552 KB |
Output is correct |
93 |
Correct |
116 ms |
21600 KB |
Output is correct |
94 |
Correct |
103 ms |
23528 KB |
Output is correct |
95 |
Correct |
120 ms |
21900 KB |
Output is correct |
96 |
Correct |
110 ms |
19788 KB |
Output is correct |
97 |
Correct |
110 ms |
17840 KB |
Output is correct |
98 |
Correct |
118 ms |
17316 KB |
Output is correct |
99 |
Correct |
131 ms |
17124 KB |
Output is correct |
100 |
Correct |
112 ms |
20856 KB |
Output is correct |
101 |
Correct |
112 ms |
18404 KB |
Output is correct |
102 |
Correct |
116 ms |
17568 KB |
Output is correct |
103 |
Correct |
134 ms |
17316 KB |
Output is correct |
104 |
Correct |
71 ms |
17600 KB |
Output is correct |
105 |
Correct |
130 ms |
28044 KB |
Output is correct |
106 |
Correct |
85 ms |
17564 KB |
Output is correct |
107 |
Correct |
77 ms |
17420 KB |
Output is correct |
108 |
Correct |
130 ms |
32740 KB |
Output is correct |
109 |
Correct |
69 ms |
17556 KB |
Output is correct |
110 |
Correct |
158 ms |
33184 KB |
Output is correct |
111 |
Correct |
120 ms |
16668 KB |
Output is correct |