#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;
Data 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();
}
return *this;
}
} f[N];
vector<int> g[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int m; cin >> m;
vector<int> c(n + m + 1);
for(int i = 2 ; i <= n + m ; ++i) {
int p;
cin >> p;
cin >> c[i];
g[p].push_back(i);
}
function<void(int)> dfs = [&](int u) {
if (u > n) {
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[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);
}; dfs(1);
cout << f[1].b + f[1].pq.top() << endl;
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
21496 KB |
Output is correct |
2 |
Correct |
24 ms |
21496 KB |
Output is correct |
3 |
Correct |
23 ms |
21496 KB |
Output is correct |
4 |
Correct |
22 ms |
21624 KB |
Output is correct |
5 |
Correct |
24 ms |
21468 KB |
Output is correct |
6 |
Correct |
23 ms |
21516 KB |
Output is correct |
7 |
Correct |
22 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21496 KB |
Output is correct |
9 |
Correct |
24 ms |
21496 KB |
Output is correct |
10 |
Correct |
24 ms |
21496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
21496 KB |
Output is correct |
2 |
Correct |
24 ms |
21496 KB |
Output is correct |
3 |
Correct |
24 ms |
21528 KB |
Output is correct |
4 |
Correct |
23 ms |
21496 KB |
Output is correct |
5 |
Correct |
23 ms |
21624 KB |
Output is correct |
6 |
Correct |
22 ms |
21496 KB |
Output is correct |
7 |
Correct |
24 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21508 KB |
Output is correct |
9 |
Correct |
24 ms |
21496 KB |
Output is correct |
10 |
Correct |
24 ms |
21496 KB |
Output is correct |
11 |
Correct |
24 ms |
21500 KB |
Output is correct |
12 |
Correct |
22 ms |
21496 KB |
Output is correct |
13 |
Correct |
24 ms |
21496 KB |
Output is correct |
14 |
Correct |
22 ms |
21496 KB |
Output is correct |
15 |
Correct |
24 ms |
21496 KB |
Output is correct |
16 |
Correct |
25 ms |
21496 KB |
Output is correct |
17 |
Correct |
21 ms |
21496 KB |
Output is correct |
18 |
Correct |
22 ms |
21520 KB |
Output is correct |
19 |
Correct |
22 ms |
21496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
21496 KB |
Output is correct |
2 |
Correct |
24 ms |
21496 KB |
Output is correct |
3 |
Correct |
23 ms |
21496 KB |
Output is correct |
4 |
Correct |
22 ms |
21624 KB |
Output is correct |
5 |
Correct |
24 ms |
21468 KB |
Output is correct |
6 |
Correct |
23 ms |
21516 KB |
Output is correct |
7 |
Correct |
22 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21496 KB |
Output is correct |
9 |
Correct |
24 ms |
21496 KB |
Output is correct |
10 |
Correct |
24 ms |
21496 KB |
Output is correct |
11 |
Correct |
25 ms |
21496 KB |
Output is correct |
12 |
Correct |
24 ms |
21496 KB |
Output is correct |
13 |
Correct |
24 ms |
21528 KB |
Output is correct |
14 |
Correct |
23 ms |
21496 KB |
Output is correct |
15 |
Correct |
23 ms |
21624 KB |
Output is correct |
16 |
Correct |
22 ms |
21496 KB |
Output is correct |
17 |
Correct |
24 ms |
21496 KB |
Output is correct |
18 |
Correct |
22 ms |
21508 KB |
Output is correct |
19 |
Correct |
24 ms |
21496 KB |
Output is correct |
20 |
Correct |
24 ms |
21496 KB |
Output is correct |
21 |
Correct |
24 ms |
21500 KB |
Output is correct |
22 |
Correct |
22 ms |
21496 KB |
Output is correct |
23 |
Correct |
24 ms |
21496 KB |
Output is correct |
24 |
Correct |
22 ms |
21496 KB |
Output is correct |
25 |
Correct |
24 ms |
21496 KB |
Output is correct |
26 |
Correct |
25 ms |
21496 KB |
Output is correct |
27 |
Correct |
21 ms |
21496 KB |
Output is correct |
28 |
Correct |
22 ms |
21520 KB |
Output is correct |
29 |
Correct |
22 ms |
21496 KB |
Output is correct |
30 |
Correct |
22 ms |
21624 KB |
Output is correct |
31 |
Correct |
22 ms |
21752 KB |
Output is correct |
32 |
Correct |
24 ms |
21780 KB |
Output is correct |
33 |
Correct |
24 ms |
21936 KB |
Output is correct |
34 |
Correct |
25 ms |
22136 KB |
Output is correct |
35 |
Correct |
27 ms |
22776 KB |
Output is correct |
36 |
Correct |
26 ms |
22652 KB |
Output is correct |
37 |
Correct |
27 ms |
22520 KB |
Output is correct |
38 |
Correct |
29 ms |
24184 KB |
Output is correct |
39 |
Correct |
30 ms |
22776 KB |
Output is correct |
40 |
Correct |
27 ms |
22648 KB |
Output is correct |
41 |
Correct |
27 ms |
22652 KB |
Output is correct |
42 |
Correct |
27 ms |
21880 KB |
Output is correct |
43 |
Correct |
56 ms |
46840 KB |
Output is correct |
44 |
Correct |
50 ms |
43896 KB |
Output is correct |
45 |
Correct |
54 ms |
43512 KB |
Output is correct |
46 |
Correct |
28 ms |
22008 KB |
Output is correct |
47 |
Correct |
31 ms |
22136 KB |
Output is correct |
48 |
Correct |
34 ms |
22064 KB |
Output is correct |
49 |
Correct |
38 ms |
22116 KB |
Output is correct |
50 |
Correct |
45 ms |
22044 KB |
Output is correct |
51 |
Correct |
43 ms |
22028 KB |
Output is correct |
52 |
Correct |
37 ms |
22584 KB |
Output is correct |
53 |
Correct |
35 ms |
22108 KB |
Output is correct |
54 |
Correct |
88 ms |
22248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
21496 KB |
Output is correct |
2 |
Correct |
24 ms |
21496 KB |
Output is correct |
3 |
Correct |
23 ms |
21496 KB |
Output is correct |
4 |
Correct |
22 ms |
21624 KB |
Output is correct |
5 |
Correct |
24 ms |
21468 KB |
Output is correct |
6 |
Correct |
23 ms |
21516 KB |
Output is correct |
7 |
Correct |
22 ms |
21496 KB |
Output is correct |
8 |
Correct |
22 ms |
21496 KB |
Output is correct |
9 |
Correct |
24 ms |
21496 KB |
Output is correct |
10 |
Correct |
24 ms |
21496 KB |
Output is correct |
11 |
Correct |
25 ms |
21496 KB |
Output is correct |
12 |
Correct |
24 ms |
21496 KB |
Output is correct |
13 |
Correct |
24 ms |
21528 KB |
Output is correct |
14 |
Correct |
23 ms |
21496 KB |
Output is correct |
15 |
Correct |
23 ms |
21624 KB |
Output is correct |
16 |
Correct |
22 ms |
21496 KB |
Output is correct |
17 |
Correct |
24 ms |
21496 KB |
Output is correct |
18 |
Correct |
22 ms |
21508 KB |
Output is correct |
19 |
Correct |
24 ms |
21496 KB |
Output is correct |
20 |
Correct |
24 ms |
21496 KB |
Output is correct |
21 |
Correct |
24 ms |
21500 KB |
Output is correct |
22 |
Correct |
22 ms |
21496 KB |
Output is correct |
23 |
Correct |
24 ms |
21496 KB |
Output is correct |
24 |
Correct |
22 ms |
21496 KB |
Output is correct |
25 |
Correct |
24 ms |
21496 KB |
Output is correct |
26 |
Correct |
25 ms |
21496 KB |
Output is correct |
27 |
Correct |
21 ms |
21496 KB |
Output is correct |
28 |
Correct |
22 ms |
21520 KB |
Output is correct |
29 |
Correct |
22 ms |
21496 KB |
Output is correct |
30 |
Correct |
22 ms |
21624 KB |
Output is correct |
31 |
Correct |
22 ms |
21752 KB |
Output is correct |
32 |
Correct |
24 ms |
21780 KB |
Output is correct |
33 |
Correct |
24 ms |
21936 KB |
Output is correct |
34 |
Correct |
25 ms |
22136 KB |
Output is correct |
35 |
Correct |
27 ms |
22776 KB |
Output is correct |
36 |
Correct |
26 ms |
22652 KB |
Output is correct |
37 |
Correct |
27 ms |
22520 KB |
Output is correct |
38 |
Correct |
29 ms |
24184 KB |
Output is correct |
39 |
Correct |
30 ms |
22776 KB |
Output is correct |
40 |
Correct |
27 ms |
22648 KB |
Output is correct |
41 |
Correct |
27 ms |
22652 KB |
Output is correct |
42 |
Correct |
27 ms |
21880 KB |
Output is correct |
43 |
Correct |
56 ms |
46840 KB |
Output is correct |
44 |
Correct |
50 ms |
43896 KB |
Output is correct |
45 |
Correct |
54 ms |
43512 KB |
Output is correct |
46 |
Correct |
28 ms |
22008 KB |
Output is correct |
47 |
Correct |
31 ms |
22136 KB |
Output is correct |
48 |
Correct |
34 ms |
22064 KB |
Output is correct |
49 |
Correct |
38 ms |
22116 KB |
Output is correct |
50 |
Correct |
45 ms |
22044 KB |
Output is correct |
51 |
Correct |
43 ms |
22028 KB |
Output is correct |
52 |
Correct |
37 ms |
22584 KB |
Output is correct |
53 |
Correct |
35 ms |
22108 KB |
Output is correct |
54 |
Correct |
88 ms |
22248 KB |
Output is correct |
55 |
Correct |
43 ms |
27220 KB |
Output is correct |
56 |
Correct |
153 ms |
76192 KB |
Output is correct |
57 |
Correct |
280 ms |
130396 KB |
Output is correct |
58 |
Correct |
327 ms |
148144 KB |
Output is correct |
59 |
Correct |
593 ms |
324584 KB |
Output is correct |
60 |
Correct |
635 ms |
302500 KB |
Output is correct |
61 |
Correct |
733 ms |
390576 KB |
Output is correct |
62 |
Correct |
816 ms |
412972 KB |
Output is correct |
63 |
Correct |
858 ms |
391784 KB |
Output is correct |
64 |
Runtime error |
934 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
65 |
Halted |
0 ms |
0 KB |
- |