#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
const int MAXN = 300011;
int p[MAXN], c[MAXN];
pair<int, int> dp[MAXN];
vector<int> G[MAXN];
int cost[MAXN], w[MAXN];
int ans = 0;
struct Firework{
Firework() = default;
Firework(int c){
c0 = c, n = 1;
v = multiset<int>{c, c};
}
int c0 = 0, n = 0;
multiset<int> v;
} fireworks[MAXN];
void operator+= (Firework& a, Firework& b){
a.c0 += b.c0; a.n += b.n;
for(auto i : b.v) a.v.insert(i);
b.v.clear();
}
void operator+= (Firework& a, int c){
a.c0 += c;
int l1 = *--a.v.end(); a.v.erase(--a.v.end());
int l2 = *--a.v.end(); a.v.erase(--a.v.end());
a.v.insert(l1 + c); a.v.insert(l2 + c);
}
void f(Firework& a){
while(a.v.size() > a.n + 1){
a.v.erase(--a.v.end());
}
}
int rt[MAXN];
void dfs1(int v){
vector<int> sons;
for(auto i : G[v]){
dfs1(i);
sons.push_back(i);
}
if(sons.size() == 0){
rt[v] = v;
fireworks[v] = Firework(0);
}else{
for(auto u : sons){
fireworks[rt[u]] += c[u];
}
for(int i = 1; i < sons.size(); i++){
if(fireworks[rt[sons[i]]].n > fireworks[rt[sons[0]]].n){
swap(sons[0], sons[i]);
}
}
for(int i = 1; i < sons.size(); i++){
fireworks[rt[sons[0]]] += fireworks[rt[sons[i]]];
}
rt[v] = rt[sons[0]];
f(fireworks[rt[v]]);
}
}
int32_t main() {
int n, m;
cin >> n >> m;
for(int i = 2; i <= n + m; i++){
cin >> p[i] >> c[i];
G[p[i]].push_back(i);
}
dfs1(1);
int ans = fireworks[rt[1]].c0, idx = 0, dx = -fireworks[rt[1]].n;
for(auto i : fireworks[rt[1]].v){
ans += dx * (i - idx); idx = i; dx++;
}
cout << ans << endl;
}
Compilation message
fireworks.cpp: In function 'void f(Firework&)':
fireworks.cpp:38:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
38 | while(a.v.size() > a.n + 1){
| ~~~~~~~~~~~^~~~~~~~~
fireworks.cpp: In function 'void dfs1(long long int)':
fireworks.cpp:58:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i = 1; i < sons.size(); i++){
| ~~^~~~~~~~~~~~~
fireworks.cpp:63:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 1; i < sons.size(); i++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
26068 KB |
Output is correct |
2 |
Correct |
13 ms |
26100 KB |
Output is correct |
3 |
Correct |
13 ms |
26152 KB |
Output is correct |
4 |
Correct |
13 ms |
26152 KB |
Output is correct |
5 |
Correct |
13 ms |
26152 KB |
Output is correct |
6 |
Correct |
13 ms |
26152 KB |
Output is correct |
7 |
Correct |
14 ms |
26072 KB |
Output is correct |
8 |
Correct |
17 ms |
26132 KB |
Output is correct |
9 |
Correct |
14 ms |
26068 KB |
Output is correct |
10 |
Correct |
14 ms |
26152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
26068 KB |
Output is correct |
2 |
Correct |
13 ms |
26120 KB |
Output is correct |
3 |
Correct |
15 ms |
26156 KB |
Output is correct |
4 |
Correct |
13 ms |
26152 KB |
Output is correct |
5 |
Correct |
14 ms |
26148 KB |
Output is correct |
6 |
Correct |
14 ms |
26144 KB |
Output is correct |
7 |
Correct |
13 ms |
26068 KB |
Output is correct |
8 |
Correct |
17 ms |
26164 KB |
Output is correct |
9 |
Correct |
16 ms |
26152 KB |
Output is correct |
10 |
Correct |
15 ms |
26068 KB |
Output is correct |
11 |
Correct |
15 ms |
26068 KB |
Output is correct |
12 |
Correct |
14 ms |
26144 KB |
Output is correct |
13 |
Correct |
13 ms |
26168 KB |
Output is correct |
14 |
Correct |
14 ms |
26148 KB |
Output is correct |
15 |
Correct |
13 ms |
26196 KB |
Output is correct |
16 |
Correct |
14 ms |
26152 KB |
Output is correct |
17 |
Correct |
13 ms |
26152 KB |
Output is correct |
18 |
Correct |
13 ms |
26152 KB |
Output is correct |
19 |
Correct |
13 ms |
26068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
26068 KB |
Output is correct |
2 |
Correct |
13 ms |
26100 KB |
Output is correct |
3 |
Correct |
13 ms |
26152 KB |
Output is correct |
4 |
Correct |
13 ms |
26152 KB |
Output is correct |
5 |
Correct |
13 ms |
26152 KB |
Output is correct |
6 |
Correct |
13 ms |
26152 KB |
Output is correct |
7 |
Correct |
14 ms |
26072 KB |
Output is correct |
8 |
Correct |
17 ms |
26132 KB |
Output is correct |
9 |
Correct |
14 ms |
26068 KB |
Output is correct |
10 |
Correct |
14 ms |
26152 KB |
Output is correct |
11 |
Correct |
12 ms |
26068 KB |
Output is correct |
12 |
Correct |
13 ms |
26120 KB |
Output is correct |
13 |
Correct |
15 ms |
26156 KB |
Output is correct |
14 |
Correct |
13 ms |
26152 KB |
Output is correct |
15 |
Correct |
14 ms |
26148 KB |
Output is correct |
16 |
Correct |
14 ms |
26144 KB |
Output is correct |
17 |
Correct |
13 ms |
26068 KB |
Output is correct |
18 |
Correct |
17 ms |
26164 KB |
Output is correct |
19 |
Correct |
16 ms |
26152 KB |
Output is correct |
20 |
Correct |
15 ms |
26068 KB |
Output is correct |
21 |
Correct |
15 ms |
26068 KB |
Output is correct |
22 |
Correct |
14 ms |
26144 KB |
Output is correct |
23 |
Correct |
13 ms |
26168 KB |
Output is correct |
24 |
Correct |
14 ms |
26148 KB |
Output is correct |
25 |
Correct |
13 ms |
26196 KB |
Output is correct |
26 |
Correct |
14 ms |
26152 KB |
Output is correct |
27 |
Correct |
13 ms |
26152 KB |
Output is correct |
28 |
Correct |
13 ms |
26152 KB |
Output is correct |
29 |
Correct |
13 ms |
26068 KB |
Output is correct |
30 |
Correct |
13 ms |
26156 KB |
Output is correct |
31 |
Correct |
17 ms |
26324 KB |
Output is correct |
32 |
Correct |
15 ms |
26272 KB |
Output is correct |
33 |
Correct |
19 ms |
26312 KB |
Output is correct |
34 |
Correct |
19 ms |
26324 KB |
Output is correct |
35 |
Correct |
16 ms |
26452 KB |
Output is correct |
36 |
Correct |
18 ms |
26420 KB |
Output is correct |
37 |
Correct |
18 ms |
26452 KB |
Output is correct |
38 |
Correct |
19 ms |
26584 KB |
Output is correct |
39 |
Correct |
18 ms |
26544 KB |
Output is correct |
40 |
Correct |
18 ms |
27348 KB |
Output is correct |
41 |
Correct |
21 ms |
27360 KB |
Output is correct |
42 |
Correct |
18 ms |
26460 KB |
Output is correct |
43 |
Correct |
18 ms |
26964 KB |
Output is correct |
44 |
Correct |
20 ms |
26836 KB |
Output is correct |
45 |
Correct |
18 ms |
26800 KB |
Output is correct |
46 |
Correct |
19 ms |
26580 KB |
Output is correct |
47 |
Correct |
18 ms |
26580 KB |
Output is correct |
48 |
Correct |
17 ms |
26580 KB |
Output is correct |
49 |
Correct |
19 ms |
26620 KB |
Output is correct |
50 |
Correct |
17 ms |
26580 KB |
Output is correct |
51 |
Correct |
18 ms |
26580 KB |
Output is correct |
52 |
Correct |
18 ms |
26580 KB |
Output is correct |
53 |
Correct |
20 ms |
26616 KB |
Output is correct |
54 |
Correct |
19 ms |
26808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
26068 KB |
Output is correct |
2 |
Correct |
13 ms |
26100 KB |
Output is correct |
3 |
Correct |
13 ms |
26152 KB |
Output is correct |
4 |
Correct |
13 ms |
26152 KB |
Output is correct |
5 |
Correct |
13 ms |
26152 KB |
Output is correct |
6 |
Correct |
13 ms |
26152 KB |
Output is correct |
7 |
Correct |
14 ms |
26072 KB |
Output is correct |
8 |
Correct |
17 ms |
26132 KB |
Output is correct |
9 |
Correct |
14 ms |
26068 KB |
Output is correct |
10 |
Correct |
14 ms |
26152 KB |
Output is correct |
11 |
Correct |
12 ms |
26068 KB |
Output is correct |
12 |
Correct |
13 ms |
26120 KB |
Output is correct |
13 |
Correct |
15 ms |
26156 KB |
Output is correct |
14 |
Correct |
13 ms |
26152 KB |
Output is correct |
15 |
Correct |
14 ms |
26148 KB |
Output is correct |
16 |
Correct |
14 ms |
26144 KB |
Output is correct |
17 |
Correct |
13 ms |
26068 KB |
Output is correct |
18 |
Correct |
17 ms |
26164 KB |
Output is correct |
19 |
Correct |
16 ms |
26152 KB |
Output is correct |
20 |
Correct |
15 ms |
26068 KB |
Output is correct |
21 |
Correct |
15 ms |
26068 KB |
Output is correct |
22 |
Correct |
14 ms |
26144 KB |
Output is correct |
23 |
Correct |
13 ms |
26168 KB |
Output is correct |
24 |
Correct |
14 ms |
26148 KB |
Output is correct |
25 |
Correct |
13 ms |
26196 KB |
Output is correct |
26 |
Correct |
14 ms |
26152 KB |
Output is correct |
27 |
Correct |
13 ms |
26152 KB |
Output is correct |
28 |
Correct |
13 ms |
26152 KB |
Output is correct |
29 |
Correct |
13 ms |
26068 KB |
Output is correct |
30 |
Correct |
13 ms |
26156 KB |
Output is correct |
31 |
Correct |
17 ms |
26324 KB |
Output is correct |
32 |
Correct |
15 ms |
26272 KB |
Output is correct |
33 |
Correct |
19 ms |
26312 KB |
Output is correct |
34 |
Correct |
19 ms |
26324 KB |
Output is correct |
35 |
Correct |
16 ms |
26452 KB |
Output is correct |
36 |
Correct |
18 ms |
26420 KB |
Output is correct |
37 |
Correct |
18 ms |
26452 KB |
Output is correct |
38 |
Correct |
19 ms |
26584 KB |
Output is correct |
39 |
Correct |
18 ms |
26544 KB |
Output is correct |
40 |
Correct |
18 ms |
27348 KB |
Output is correct |
41 |
Correct |
21 ms |
27360 KB |
Output is correct |
42 |
Correct |
18 ms |
26460 KB |
Output is correct |
43 |
Correct |
18 ms |
26964 KB |
Output is correct |
44 |
Correct |
20 ms |
26836 KB |
Output is correct |
45 |
Correct |
18 ms |
26800 KB |
Output is correct |
46 |
Correct |
19 ms |
26580 KB |
Output is correct |
47 |
Correct |
18 ms |
26580 KB |
Output is correct |
48 |
Correct |
17 ms |
26580 KB |
Output is correct |
49 |
Correct |
19 ms |
26620 KB |
Output is correct |
50 |
Correct |
17 ms |
26580 KB |
Output is correct |
51 |
Correct |
18 ms |
26580 KB |
Output is correct |
52 |
Correct |
18 ms |
26580 KB |
Output is correct |
53 |
Correct |
20 ms |
26616 KB |
Output is correct |
54 |
Correct |
19 ms |
26808 KB |
Output is correct |
55 |
Correct |
29 ms |
27180 KB |
Output is correct |
56 |
Correct |
70 ms |
30024 KB |
Output is correct |
57 |
Correct |
110 ms |
32776 KB |
Output is correct |
58 |
Correct |
152 ms |
34888 KB |
Output is correct |
59 |
Correct |
203 ms |
37836 KB |
Output is correct |
60 |
Correct |
265 ms |
40520 KB |
Output is correct |
61 |
Correct |
299 ms |
42660 KB |
Output is correct |
62 |
Correct |
352 ms |
45056 KB |
Output is correct |
63 |
Correct |
404 ms |
48340 KB |
Output is correct |
64 |
Correct |
439 ms |
48796 KB |
Output is correct |
65 |
Correct |
279 ms |
99052 KB |
Output is correct |
66 |
Correct |
288 ms |
99048 KB |
Output is correct |
67 |
Correct |
261 ms |
47604 KB |
Output is correct |
68 |
Correct |
393 ms |
75488 KB |
Output is correct |
69 |
Correct |
405 ms |
71936 KB |
Output is correct |
70 |
Correct |
396 ms |
71836 KB |
Output is correct |
71 |
Correct |
462 ms |
55756 KB |
Output is correct |
72 |
Correct |
498 ms |
55580 KB |
Output is correct |
73 |
Correct |
418 ms |
54500 KB |
Output is correct |
74 |
Correct |
475 ms |
54512 KB |
Output is correct |
75 |
Correct |
451 ms |
54084 KB |
Output is correct |
76 |
Correct |
423 ms |
54156 KB |
Output is correct |
77 |
Correct |
466 ms |
54712 KB |
Output is correct |
78 |
Correct |
483 ms |
54576 KB |
Output is correct |
79 |
Correct |
492 ms |
69556 KB |
Output is correct |