#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5 + 1;
struct Data {
int a, b;
priority_queue<int> pq;
void operator += (Data &x) {
a += x.a;
b += x.b;
if (pq.size() < x.pq.size())
pq.swap(x.pq);
while (x.pq.size()) {
pq.push(x.pq.top());
x.pq.pop();
}
}
} f[N];
int c[N];
vector<int> g[N];
void dfs(int u) {
if (g[u].empty()) {
f[u].a = 1;
f[u].b = -c[u];
f[u].pq.push(c[u]);
f[u].pq.push(c[u]);
return;
}
for(int v : g[u])
dfs(v), f[u] += f[v];
for(; f[u].a > 1 ;)
f[u].a--,
f[u].b += f[u].pq.top(),
f[u].pq.pop();
int x = f[u].pq.top() + c[u]; f[u].pq.pop();
int y = f[u].pq.top() + c[u]; f[u].pq.pop();
f[u].b -= c[u];
f[u].pq.push(x);
f[u].pq.push(y);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int m; cin >> m;
for(int i = 2 ; i <= n + m ; ++i) {
int p;
cin >> p;
cin >> c[i];
g[p].push_back(i);
}
dfs(1); cout << f[1].b + f[1].pq.top() << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21476 KB |
Output is correct |
3 |
Correct |
21 ms |
21496 KB |
Output is correct |
4 |
Correct |
22 ms |
21496 KB |
Output is correct |
5 |
Correct |
21 ms |
21496 KB |
Output is correct |
6 |
Correct |
22 ms |
21496 KB |
Output is correct |
7 |
Correct |
21 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21448 KB |
Output is correct |
9 |
Correct |
21 ms |
21496 KB |
Output is correct |
10 |
Correct |
45 ms |
21468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
21496 KB |
Output is correct |
2 |
Correct |
24 ms |
21496 KB |
Output is correct |
3 |
Correct |
21 ms |
21496 KB |
Output is correct |
4 |
Correct |
21 ms |
21496 KB |
Output is correct |
5 |
Correct |
22 ms |
21496 KB |
Output is correct |
6 |
Correct |
23 ms |
21496 KB |
Output is correct |
7 |
Correct |
23 ms |
21496 KB |
Output is correct |
8 |
Correct |
24 ms |
21496 KB |
Output is correct |
9 |
Correct |
22 ms |
21496 KB |
Output is correct |
10 |
Correct |
23 ms |
21496 KB |
Output is correct |
11 |
Correct |
23 ms |
21496 KB |
Output is correct |
12 |
Correct |
23 ms |
21496 KB |
Output is correct |
13 |
Correct |
24 ms |
21496 KB |
Output is correct |
14 |
Correct |
23 ms |
21496 KB |
Output is correct |
15 |
Correct |
23 ms |
21496 KB |
Output is correct |
16 |
Correct |
23 ms |
21496 KB |
Output is correct |
17 |
Correct |
24 ms |
21496 KB |
Output is correct |
18 |
Correct |
24 ms |
21496 KB |
Output is correct |
19 |
Correct |
21 ms |
21496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21476 KB |
Output is correct |
3 |
Correct |
21 ms |
21496 KB |
Output is correct |
4 |
Correct |
22 ms |
21496 KB |
Output is correct |
5 |
Correct |
21 ms |
21496 KB |
Output is correct |
6 |
Correct |
22 ms |
21496 KB |
Output is correct |
7 |
Correct |
21 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21448 KB |
Output is correct |
9 |
Correct |
21 ms |
21496 KB |
Output is correct |
10 |
Correct |
45 ms |
21468 KB |
Output is correct |
11 |
Correct |
21 ms |
21496 KB |
Output is correct |
12 |
Correct |
24 ms |
21496 KB |
Output is correct |
13 |
Correct |
21 ms |
21496 KB |
Output is correct |
14 |
Correct |
21 ms |
21496 KB |
Output is correct |
15 |
Correct |
22 ms |
21496 KB |
Output is correct |
16 |
Correct |
23 ms |
21496 KB |
Output is correct |
17 |
Correct |
23 ms |
21496 KB |
Output is correct |
18 |
Correct |
24 ms |
21496 KB |
Output is correct |
19 |
Correct |
22 ms |
21496 KB |
Output is correct |
20 |
Correct |
23 ms |
21496 KB |
Output is correct |
21 |
Correct |
23 ms |
21496 KB |
Output is correct |
22 |
Correct |
23 ms |
21496 KB |
Output is correct |
23 |
Correct |
24 ms |
21496 KB |
Output is correct |
24 |
Correct |
23 ms |
21496 KB |
Output is correct |
25 |
Correct |
23 ms |
21496 KB |
Output is correct |
26 |
Correct |
23 ms |
21496 KB |
Output is correct |
27 |
Correct |
24 ms |
21496 KB |
Output is correct |
28 |
Correct |
24 ms |
21496 KB |
Output is correct |
29 |
Correct |
21 ms |
21496 KB |
Output is correct |
30 |
Correct |
26 ms |
21540 KB |
Output is correct |
31 |
Correct |
22 ms |
21496 KB |
Output is correct |
32 |
Correct |
22 ms |
21496 KB |
Output is correct |
33 |
Correct |
25 ms |
21624 KB |
Output is correct |
34 |
Correct |
24 ms |
21624 KB |
Output is correct |
35 |
Correct |
24 ms |
21624 KB |
Output is correct |
36 |
Correct |
24 ms |
21752 KB |
Output is correct |
37 |
Correct |
25 ms |
21752 KB |
Output is correct |
38 |
Correct |
25 ms |
21756 KB |
Output is correct |
39 |
Correct |
25 ms |
21752 KB |
Output is correct |
40 |
Correct |
24 ms |
22264 KB |
Output is correct |
41 |
Correct |
24 ms |
22264 KB |
Output is correct |
42 |
Correct |
23 ms |
21752 KB |
Output is correct |
43 |
Correct |
24 ms |
21984 KB |
Output is correct |
44 |
Correct |
25 ms |
22008 KB |
Output is correct |
45 |
Correct |
24 ms |
21880 KB |
Output is correct |
46 |
Correct |
24 ms |
21884 KB |
Output is correct |
47 |
Correct |
25 ms |
21880 KB |
Output is correct |
48 |
Correct |
24 ms |
21880 KB |
Output is correct |
49 |
Correct |
25 ms |
21880 KB |
Output is correct |
50 |
Correct |
24 ms |
21752 KB |
Output is correct |
51 |
Correct |
23 ms |
21752 KB |
Output is correct |
52 |
Correct |
24 ms |
21752 KB |
Output is correct |
53 |
Correct |
24 ms |
21884 KB |
Output is correct |
54 |
Correct |
23 ms |
21880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21476 KB |
Output is correct |
3 |
Correct |
21 ms |
21496 KB |
Output is correct |
4 |
Correct |
22 ms |
21496 KB |
Output is correct |
5 |
Correct |
21 ms |
21496 KB |
Output is correct |
6 |
Correct |
22 ms |
21496 KB |
Output is correct |
7 |
Correct |
21 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21448 KB |
Output is correct |
9 |
Correct |
21 ms |
21496 KB |
Output is correct |
10 |
Correct |
45 ms |
21468 KB |
Output is correct |
11 |
Correct |
21 ms |
21496 KB |
Output is correct |
12 |
Correct |
24 ms |
21496 KB |
Output is correct |
13 |
Correct |
21 ms |
21496 KB |
Output is correct |
14 |
Correct |
21 ms |
21496 KB |
Output is correct |
15 |
Correct |
22 ms |
21496 KB |
Output is correct |
16 |
Correct |
23 ms |
21496 KB |
Output is correct |
17 |
Correct |
23 ms |
21496 KB |
Output is correct |
18 |
Correct |
24 ms |
21496 KB |
Output is correct |
19 |
Correct |
22 ms |
21496 KB |
Output is correct |
20 |
Correct |
23 ms |
21496 KB |
Output is correct |
21 |
Correct |
23 ms |
21496 KB |
Output is correct |
22 |
Correct |
23 ms |
21496 KB |
Output is correct |
23 |
Correct |
24 ms |
21496 KB |
Output is correct |
24 |
Correct |
23 ms |
21496 KB |
Output is correct |
25 |
Correct |
23 ms |
21496 KB |
Output is correct |
26 |
Correct |
23 ms |
21496 KB |
Output is correct |
27 |
Correct |
24 ms |
21496 KB |
Output is correct |
28 |
Correct |
24 ms |
21496 KB |
Output is correct |
29 |
Correct |
21 ms |
21496 KB |
Output is correct |
30 |
Correct |
26 ms |
21540 KB |
Output is correct |
31 |
Correct |
22 ms |
21496 KB |
Output is correct |
32 |
Correct |
22 ms |
21496 KB |
Output is correct |
33 |
Correct |
25 ms |
21624 KB |
Output is correct |
34 |
Correct |
24 ms |
21624 KB |
Output is correct |
35 |
Correct |
24 ms |
21624 KB |
Output is correct |
36 |
Correct |
24 ms |
21752 KB |
Output is correct |
37 |
Correct |
25 ms |
21752 KB |
Output is correct |
38 |
Correct |
25 ms |
21756 KB |
Output is correct |
39 |
Correct |
25 ms |
21752 KB |
Output is correct |
40 |
Correct |
24 ms |
22264 KB |
Output is correct |
41 |
Correct |
24 ms |
22264 KB |
Output is correct |
42 |
Correct |
23 ms |
21752 KB |
Output is correct |
43 |
Correct |
24 ms |
21984 KB |
Output is correct |
44 |
Correct |
25 ms |
22008 KB |
Output is correct |
45 |
Correct |
24 ms |
21880 KB |
Output is correct |
46 |
Correct |
24 ms |
21884 KB |
Output is correct |
47 |
Correct |
25 ms |
21880 KB |
Output is correct |
48 |
Correct |
24 ms |
21880 KB |
Output is correct |
49 |
Correct |
25 ms |
21880 KB |
Output is correct |
50 |
Correct |
24 ms |
21752 KB |
Output is correct |
51 |
Correct |
23 ms |
21752 KB |
Output is correct |
52 |
Correct |
24 ms |
21752 KB |
Output is correct |
53 |
Correct |
24 ms |
21884 KB |
Output is correct |
54 |
Correct |
23 ms |
21880 KB |
Output is correct |
55 |
Correct |
29 ms |
22264 KB |
Output is correct |
56 |
Correct |
60 ms |
24284 KB |
Output is correct |
57 |
Correct |
99 ms |
26156 KB |
Output is correct |
58 |
Correct |
142 ms |
27520 KB |
Output is correct |
59 |
Correct |
188 ms |
29524 KB |
Output is correct |
60 |
Correct |
226 ms |
31600 KB |
Output is correct |
61 |
Correct |
247 ms |
33400 KB |
Output is correct |
62 |
Correct |
290 ms |
34416 KB |
Output is correct |
63 |
Correct |
343 ms |
36724 KB |
Output is correct |
64 |
Correct |
340 ms |
37344 KB |
Output is correct |
65 |
Correct |
166 ms |
66168 KB |
Output is correct |
66 |
Correct |
168 ms |
66168 KB |
Output is correct |
67 |
Correct |
184 ms |
33296 KB |
Output is correct |
68 |
Correct |
213 ms |
51180 KB |
Output is correct |
69 |
Correct |
267 ms |
53436 KB |
Output is correct |
70 |
Correct |
267 ms |
53460 KB |
Output is correct |
71 |
Correct |
255 ms |
52448 KB |
Output is correct |
72 |
Correct |
251 ms |
52856 KB |
Output is correct |
73 |
Correct |
215 ms |
48732 KB |
Output is correct |
74 |
Correct |
219 ms |
48784 KB |
Output is correct |
75 |
Correct |
217 ms |
47504 KB |
Output is correct |
76 |
Correct |
249 ms |
47464 KB |
Output is correct |
77 |
Correct |
239 ms |
47144 KB |
Output is correct |
78 |
Correct |
224 ms |
45240 KB |
Output is correct |
79 |
Correct |
363 ms |
46212 KB |
Output is correct |