#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long llint;
const int maxn = 3e5+10;
int n, m;
int p[maxn], c[maxn];
vector< pair<int, int> > graph[maxn];
multiset< llint > s[maxn];
llint a[maxn], b[maxn];
void solve(int x) {
//printf("solving: %d\n", x);
for (auto iter : graph[x]) {
int tren = iter.X;
int cost = iter.Y;
solve(tren);
while (a[tren] > 1) {
a[tren]--;
b[tren] += *s[tren].rbegin();
s[tren].erase(--s[tren].end());
}
b[tren] -= cost;
if (tren > n) {
a[tren] = 1;
s[tren].insert(cost);
s[tren].insert(cost);
} else {
llint a = *s[tren].rbegin();
s[tren].erase(--s[tren].end());
llint b = *s[tren].rbegin();
s[tren].erase(--s[tren].end());
s[tren].insert(a + cost);
s[tren].insert(b + cost);
}
//printf("merging: %d (%lld %lld)\n", tren, a[tren], b[tren]);
//for (auto iter : s[tren]) printf("%lld ", iter); printf("\n");
a[x] += a[tren];
b[x] += b[tren];
if (s[x].size() < s[tren].size()) swap(s[x], s[tren]);
for (auto iter : s[tren]) s[x].insert(iter);
}
//printf("solved: %d\n", x);
//printf("%lld %lld\n", a[x], b[x]);
//for (llint tren : s[x]) printf("%lld ", tren); printf("\n");
}
int main() {
scanf("%d%d", &n, &m); m += n;
for (int i = 2; i <= m; i++) {
scanf("%d%d", p+i, c+i);
graph[p[i]].push_back({i, c[i]});
}
solve(1);
llint sol = b[1];
while (a[1] > 0) {
a[1]--;
sol += *s[1].rbegin();
s[1].erase(--s[1].end());
}
printf("%lld\n", sol);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d%d", &n, &m); m += n;
| ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", p+i, c+i);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
21432 KB |
Output is correct |
2 |
Correct |
12 ms |
21452 KB |
Output is correct |
3 |
Correct |
14 ms |
21480 KB |
Output is correct |
4 |
Correct |
11 ms |
21436 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
12 ms |
21452 KB |
Output is correct |
7 |
Correct |
11 ms |
21392 KB |
Output is correct |
8 |
Correct |
11 ms |
21452 KB |
Output is correct |
9 |
Correct |
11 ms |
21460 KB |
Output is correct |
10 |
Correct |
13 ms |
21460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
21452 KB |
Output is correct |
2 |
Correct |
10 ms |
21452 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21444 KB |
Output is correct |
5 |
Correct |
11 ms |
21404 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21452 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
11 ms |
21452 KB |
Output is correct |
10 |
Correct |
11 ms |
21460 KB |
Output is correct |
11 |
Correct |
11 ms |
21428 KB |
Output is correct |
12 |
Correct |
11 ms |
21504 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21476 KB |
Output is correct |
16 |
Correct |
11 ms |
21452 KB |
Output is correct |
17 |
Correct |
14 ms |
21460 KB |
Output is correct |
18 |
Correct |
13 ms |
21428 KB |
Output is correct |
19 |
Correct |
11 ms |
21400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
21432 KB |
Output is correct |
2 |
Correct |
12 ms |
21452 KB |
Output is correct |
3 |
Correct |
14 ms |
21480 KB |
Output is correct |
4 |
Correct |
11 ms |
21436 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
12 ms |
21452 KB |
Output is correct |
7 |
Correct |
11 ms |
21392 KB |
Output is correct |
8 |
Correct |
11 ms |
21452 KB |
Output is correct |
9 |
Correct |
11 ms |
21460 KB |
Output is correct |
10 |
Correct |
13 ms |
21460 KB |
Output is correct |
11 |
Correct |
10 ms |
21452 KB |
Output is correct |
12 |
Correct |
10 ms |
21452 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
10 ms |
21444 KB |
Output is correct |
15 |
Correct |
11 ms |
21404 KB |
Output is correct |
16 |
Correct |
11 ms |
21460 KB |
Output is correct |
17 |
Correct |
11 ms |
21452 KB |
Output is correct |
18 |
Correct |
11 ms |
21460 KB |
Output is correct |
19 |
Correct |
11 ms |
21452 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
11 ms |
21428 KB |
Output is correct |
22 |
Correct |
11 ms |
21504 KB |
Output is correct |
23 |
Correct |
11 ms |
21460 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
11 ms |
21476 KB |
Output is correct |
26 |
Correct |
11 ms |
21452 KB |
Output is correct |
27 |
Correct |
14 ms |
21460 KB |
Output is correct |
28 |
Correct |
13 ms |
21428 KB |
Output is correct |
29 |
Correct |
11 ms |
21400 KB |
Output is correct |
30 |
Correct |
10 ms |
21456 KB |
Output is correct |
31 |
Correct |
11 ms |
21588 KB |
Output is correct |
32 |
Correct |
11 ms |
21588 KB |
Output is correct |
33 |
Correct |
11 ms |
21780 KB |
Output is correct |
34 |
Correct |
13 ms |
21780 KB |
Output is correct |
35 |
Correct |
13 ms |
21928 KB |
Output is correct |
36 |
Correct |
13 ms |
21972 KB |
Output is correct |
37 |
Correct |
13 ms |
22164 KB |
Output is correct |
38 |
Correct |
14 ms |
22104 KB |
Output is correct |
39 |
Correct |
13 ms |
22200 KB |
Output is correct |
40 |
Correct |
12 ms |
22484 KB |
Output is correct |
41 |
Correct |
15 ms |
22516 KB |
Output is correct |
42 |
Correct |
15 ms |
21736 KB |
Output is correct |
43 |
Correct |
14 ms |
22364 KB |
Output is correct |
44 |
Correct |
13 ms |
22300 KB |
Output is correct |
45 |
Correct |
13 ms |
22356 KB |
Output is correct |
46 |
Correct |
13 ms |
22612 KB |
Output is correct |
47 |
Correct |
14 ms |
22612 KB |
Output is correct |
48 |
Correct |
15 ms |
22364 KB |
Output is correct |
49 |
Correct |
14 ms |
22484 KB |
Output is correct |
50 |
Correct |
13 ms |
22356 KB |
Output is correct |
51 |
Correct |
16 ms |
22228 KB |
Output is correct |
52 |
Correct |
13 ms |
22356 KB |
Output is correct |
53 |
Correct |
14 ms |
22356 KB |
Output is correct |
54 |
Correct |
17 ms |
22612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
21432 KB |
Output is correct |
2 |
Correct |
12 ms |
21452 KB |
Output is correct |
3 |
Correct |
14 ms |
21480 KB |
Output is correct |
4 |
Correct |
11 ms |
21436 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
12 ms |
21452 KB |
Output is correct |
7 |
Correct |
11 ms |
21392 KB |
Output is correct |
8 |
Correct |
11 ms |
21452 KB |
Output is correct |
9 |
Correct |
11 ms |
21460 KB |
Output is correct |
10 |
Correct |
13 ms |
21460 KB |
Output is correct |
11 |
Correct |
10 ms |
21452 KB |
Output is correct |
12 |
Correct |
10 ms |
21452 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
10 ms |
21444 KB |
Output is correct |
15 |
Correct |
11 ms |
21404 KB |
Output is correct |
16 |
Correct |
11 ms |
21460 KB |
Output is correct |
17 |
Correct |
11 ms |
21452 KB |
Output is correct |
18 |
Correct |
11 ms |
21460 KB |
Output is correct |
19 |
Correct |
11 ms |
21452 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
11 ms |
21428 KB |
Output is correct |
22 |
Correct |
11 ms |
21504 KB |
Output is correct |
23 |
Correct |
11 ms |
21460 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
11 ms |
21476 KB |
Output is correct |
26 |
Correct |
11 ms |
21452 KB |
Output is correct |
27 |
Correct |
14 ms |
21460 KB |
Output is correct |
28 |
Correct |
13 ms |
21428 KB |
Output is correct |
29 |
Correct |
11 ms |
21400 KB |
Output is correct |
30 |
Correct |
10 ms |
21456 KB |
Output is correct |
31 |
Correct |
11 ms |
21588 KB |
Output is correct |
32 |
Correct |
11 ms |
21588 KB |
Output is correct |
33 |
Correct |
11 ms |
21780 KB |
Output is correct |
34 |
Correct |
13 ms |
21780 KB |
Output is correct |
35 |
Correct |
13 ms |
21928 KB |
Output is correct |
36 |
Correct |
13 ms |
21972 KB |
Output is correct |
37 |
Correct |
13 ms |
22164 KB |
Output is correct |
38 |
Correct |
14 ms |
22104 KB |
Output is correct |
39 |
Correct |
13 ms |
22200 KB |
Output is correct |
40 |
Correct |
12 ms |
22484 KB |
Output is correct |
41 |
Correct |
15 ms |
22516 KB |
Output is correct |
42 |
Correct |
15 ms |
21736 KB |
Output is correct |
43 |
Correct |
14 ms |
22364 KB |
Output is correct |
44 |
Correct |
13 ms |
22300 KB |
Output is correct |
45 |
Correct |
13 ms |
22356 KB |
Output is correct |
46 |
Correct |
13 ms |
22612 KB |
Output is correct |
47 |
Correct |
14 ms |
22612 KB |
Output is correct |
48 |
Correct |
15 ms |
22364 KB |
Output is correct |
49 |
Correct |
14 ms |
22484 KB |
Output is correct |
50 |
Correct |
13 ms |
22356 KB |
Output is correct |
51 |
Correct |
16 ms |
22228 KB |
Output is correct |
52 |
Correct |
13 ms |
22356 KB |
Output is correct |
53 |
Correct |
14 ms |
22356 KB |
Output is correct |
54 |
Correct |
17 ms |
22612 KB |
Output is correct |
55 |
Correct |
17 ms |
23288 KB |
Output is correct |
56 |
Correct |
45 ms |
29040 KB |
Output is correct |
57 |
Correct |
69 ms |
34904 KB |
Output is correct |
58 |
Correct |
85 ms |
38808 KB |
Output is correct |
59 |
Correct |
120 ms |
44536 KB |
Output is correct |
60 |
Correct |
151 ms |
50440 KB |
Output is correct |
61 |
Correct |
175 ms |
54480 KB |
Output is correct |
62 |
Correct |
214 ms |
58496 KB |
Output is correct |
63 |
Correct |
239 ms |
66504 KB |
Output is correct |
64 |
Correct |
265 ms |
67412 KB |
Output is correct |
65 |
Correct |
117 ms |
89592 KB |
Output is correct |
66 |
Correct |
120 ms |
89608 KB |
Output is correct |
67 |
Correct |
123 ms |
42856 KB |
Output is correct |
68 |
Correct |
177 ms |
82496 KB |
Output is correct |
69 |
Correct |
231 ms |
79628 KB |
Output is correct |
70 |
Correct |
227 ms |
79636 KB |
Output is correct |
71 |
Correct |
332 ms |
105340 KB |
Output is correct |
72 |
Correct |
347 ms |
104688 KB |
Output is correct |
73 |
Correct |
313 ms |
91680 KB |
Output is correct |
74 |
Correct |
338 ms |
91852 KB |
Output is correct |
75 |
Correct |
383 ms |
90864 KB |
Output is correct |
76 |
Correct |
375 ms |
91068 KB |
Output is correct |
77 |
Correct |
357 ms |
88912 KB |
Output is correct |
78 |
Correct |
343 ms |
86136 KB |
Output is correct |
79 |
Correct |
345 ms |
90664 KB |
Output is correct |