#include <bits/stdc++.h>
using namespace std;
const int DIM = 300005;
struct Dp {
long long a, b;
priority_queue<long long> *points;
};
Dp dp[DIM];
int parent[DIM], length[DIM];
Dp merge(Dp str1, Dp str2) {
Dp ans;
ans.a = str1.a + str2.a;
ans.b = str1.b + str2.b;
if (str1.points -> size() > str2.points -> size()) {
ans.points = str1.points;
while (str2.points -> size()) {
ans.points -> push(str2.points -> top());
str2.points -> pop();
}
} else {
ans.points = str2.points;
while (str1.points -> size()) {
ans.points -> push(str1.points -> top());
str1.points -> pop();
}
}
return ans;
}
int main(void) {
// freopen("fireworks.in", "r", stdin);
// freopen("fireworks.out", "w", stdout);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n + m; ++i) {
if (i > 1)
cin >> parent[i] >> length[i];
dp[i].points = new priority_queue<long long>();
}
for (int i = n + m; i > 1; --i) {
if (i > n) {
dp[i].a = 1; dp[i].b = -length[i];
dp[i].points -> push(length[i]);
dp[i].points -> push(length[i]);
} else {
while (dp[i].a > 1) {
long long x = dp[i].points -> top();
dp[i].a--; dp[i].b += x;
dp[i].points -> pop();
}
long long x = dp[i].points -> top();
dp[i].points -> pop();
long long y = dp[i].points -> top();
dp[i].points -> pop();
dp[i].points -> push(x + length[i]);
dp[i].points -> push(y + length[i]);
dp[i].b -= length[i];
}
dp[parent[i]] = merge(dp[parent[i]], dp[i]);
}
while (dp[1].a > 0) {
long long x = dp[1].points -> top();
dp[1].a--; dp[1].b += x;
dp[1].points -> pop();
}
cout << dp[1].b << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
3 ms |
256 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
5 ms |
640 KB |
Output is correct |
34 |
Correct |
5 ms |
640 KB |
Output is correct |
35 |
Correct |
6 ms |
768 KB |
Output is correct |
36 |
Correct |
6 ms |
896 KB |
Output is correct |
37 |
Correct |
7 ms |
1024 KB |
Output is correct |
38 |
Correct |
7 ms |
1024 KB |
Output is correct |
39 |
Correct |
9 ms |
896 KB |
Output is correct |
40 |
Correct |
6 ms |
768 KB |
Output is correct |
41 |
Correct |
11 ms |
768 KB |
Output is correct |
42 |
Correct |
7 ms |
896 KB |
Output is correct |
43 |
Correct |
7 ms |
896 KB |
Output is correct |
44 |
Correct |
7 ms |
1024 KB |
Output is correct |
45 |
Correct |
7 ms |
1024 KB |
Output is correct |
46 |
Correct |
7 ms |
1152 KB |
Output is correct |
47 |
Correct |
7 ms |
1152 KB |
Output is correct |
48 |
Correct |
6 ms |
1024 KB |
Output is correct |
49 |
Correct |
7 ms |
1152 KB |
Output is correct |
50 |
Correct |
8 ms |
1024 KB |
Output is correct |
51 |
Correct |
6 ms |
896 KB |
Output is correct |
52 |
Correct |
6 ms |
1024 KB |
Output is correct |
53 |
Correct |
7 ms |
1024 KB |
Output is correct |
54 |
Correct |
6 ms |
1020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
3 ms |
256 KB |
Output is correct |
31 |
Correct |
3 ms |
512 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
5 ms |
640 KB |
Output is correct |
34 |
Correct |
5 ms |
640 KB |
Output is correct |
35 |
Correct |
6 ms |
768 KB |
Output is correct |
36 |
Correct |
6 ms |
896 KB |
Output is correct |
37 |
Correct |
7 ms |
1024 KB |
Output is correct |
38 |
Correct |
7 ms |
1024 KB |
Output is correct |
39 |
Correct |
9 ms |
896 KB |
Output is correct |
40 |
Correct |
6 ms |
768 KB |
Output is correct |
41 |
Correct |
11 ms |
768 KB |
Output is correct |
42 |
Correct |
7 ms |
896 KB |
Output is correct |
43 |
Correct |
7 ms |
896 KB |
Output is correct |
44 |
Correct |
7 ms |
1024 KB |
Output is correct |
45 |
Correct |
7 ms |
1024 KB |
Output is correct |
46 |
Correct |
7 ms |
1152 KB |
Output is correct |
47 |
Correct |
7 ms |
1152 KB |
Output is correct |
48 |
Correct |
6 ms |
1024 KB |
Output is correct |
49 |
Correct |
7 ms |
1152 KB |
Output is correct |
50 |
Correct |
8 ms |
1024 KB |
Output is correct |
51 |
Correct |
6 ms |
896 KB |
Output is correct |
52 |
Correct |
6 ms |
1024 KB |
Output is correct |
53 |
Correct |
7 ms |
1024 KB |
Output is correct |
54 |
Correct |
6 ms |
1020 KB |
Output is correct |
55 |
Correct |
15 ms |
2088 KB |
Output is correct |
56 |
Correct |
52 ms |
6896 KB |
Output is correct |
57 |
Correct |
89 ms |
11388 KB |
Output is correct |
58 |
Correct |
116 ms |
14424 KB |
Output is correct |
59 |
Correct |
159 ms |
18940 KB |
Output is correct |
60 |
Correct |
199 ms |
23516 KB |
Output is correct |
61 |
Correct |
231 ms |
26864 KB |
Output is correct |
62 |
Correct |
263 ms |
29864 KB |
Output is correct |
63 |
Correct |
314 ms |
35300 KB |
Output is correct |
64 |
Correct |
364 ms |
36972 KB |
Output is correct |
65 |
Correct |
244 ms |
28792 KB |
Output is correct |
66 |
Correct |
244 ms |
28792 KB |
Output is correct |
67 |
Correct |
252 ms |
28740 KB |
Output is correct |
68 |
Correct |
294 ms |
35404 KB |
Output is correct |
69 |
Correct |
312 ms |
36080 KB |
Output is correct |
70 |
Correct |
314 ms |
36204 KB |
Output is correct |
71 |
Correct |
333 ms |
50988 KB |
Output is correct |
72 |
Correct |
340 ms |
51180 KB |
Output is correct |
73 |
Correct |
306 ms |
47356 KB |
Output is correct |
74 |
Correct |
334 ms |
48100 KB |
Output is correct |
75 |
Correct |
302 ms |
46324 KB |
Output is correct |
76 |
Correct |
297 ms |
46308 KB |
Output is correct |
77 |
Correct |
318 ms |
45456 KB |
Output is correct |
78 |
Correct |
314 ms |
44972 KB |
Output is correct |
79 |
Correct |
311 ms |
43692 KB |
Output is correct |