#include <bits/stdc++.h>
#include <utility>
using namespace std;
#define int long long
struct line{
int m,n;
multiset<int> points;
line():n(0),m(0),points(){}
line(int m,int n,multiset<int> points):m(m),n(n),points(points){}
void operator+=(const line &other){
m+=other.m;
n+=other.n;
for(int i:other.points)points.insert(i);
}
};
int n,m;
vector<pair<int,int>> adj[300001];
line functions[300001];
void dfs(int x,int p){
if(x>n) {
functions[x] = {1, -adj[x][0].second, {adj[x][0].second, adj[x][0].second}};
return;
}
int len = 0;
for(auto&i:adj[x]){
if(i.first!=p){
dfs(i.first,x);
if(functions[i.first].points.size()>functions[x].points.size())swap(functions[x],functions[i.first]);
functions[x]+=functions[i.first];
}
else len = i.second;
}
int currsum = functions[x].n;
int currslope = functions[x].m;
for(auto i=--functions[x].points.end();i!=functions[x].points.begin();i--){
currslope--;
if(currslope==-1)break;
currsum+=*i;
}
while(functions[x].m>1){
functions[x].m--;
functions[x].points.erase(--functions[x].points.end());
}
int f = *--functions[x].points.end();
functions[x].points.erase(--functions[x].points.end());
int s = *--functions[x].points.end();
functions[x].points.erase(--functions[x].points.end());
functions[x].points.insert(s+len);
functions[x].points.insert(f+len);
functions[x].n = currsum-(f+len);
functions[x] = functions[x];
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i=2;i<=n+m;i++){
int p,c;cin>>p>>c;
adj[i].emplace_back(p,c);
adj[p].emplace_back(i,c);
}
dfs(1,0);
line ans = functions[1];
int currsum = ans.n;
int currslope = ans.m;
for(auto i=--ans.points.end();i!=ans.points.begin();i--){
currslope--;
if(currslope==-1)break;
currsum+=*i;
}
cout << currsum << '\n';
}
Compilation message
fireworks.cpp: In constructor 'line::line()':
fireworks.cpp:9:11: warning: 'line::n' will be initialized after [-Wreorder]
9 | int m,n;
| ^
fireworks.cpp:9:9: warning: 'long long int line::m' [-Wreorder]
9 | int m,n;
| ^
fireworks.cpp:11:5: warning: when initialized here [-Wreorder]
11 | line():n(0),m(0),points(){}
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
26200 KB |
Output is correct |
2 |
Correct |
8 ms |
26204 KB |
Output is correct |
3 |
Correct |
6 ms |
26244 KB |
Output is correct |
4 |
Correct |
6 ms |
26204 KB |
Output is correct |
5 |
Correct |
7 ms |
26236 KB |
Output is correct |
6 |
Correct |
6 ms |
26204 KB |
Output is correct |
7 |
Correct |
6 ms |
26292 KB |
Output is correct |
8 |
Correct |
6 ms |
26204 KB |
Output is correct |
9 |
Correct |
6 ms |
26204 KB |
Output is correct |
10 |
Correct |
6 ms |
26204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26204 KB |
Output is correct |
2 |
Correct |
7 ms |
26204 KB |
Output is correct |
3 |
Correct |
6 ms |
26204 KB |
Output is correct |
4 |
Correct |
6 ms |
26204 KB |
Output is correct |
5 |
Correct |
6 ms |
26228 KB |
Output is correct |
6 |
Correct |
6 ms |
26200 KB |
Output is correct |
7 |
Correct |
6 ms |
26204 KB |
Output is correct |
8 |
Correct |
6 ms |
26200 KB |
Output is correct |
9 |
Correct |
6 ms |
26204 KB |
Output is correct |
10 |
Correct |
6 ms |
26204 KB |
Output is correct |
11 |
Correct |
6 ms |
26200 KB |
Output is correct |
12 |
Correct |
6 ms |
26456 KB |
Output is correct |
13 |
Correct |
6 ms |
26204 KB |
Output is correct |
14 |
Correct |
6 ms |
26204 KB |
Output is correct |
15 |
Correct |
7 ms |
26204 KB |
Output is correct |
16 |
Correct |
6 ms |
26204 KB |
Output is correct |
17 |
Correct |
6 ms |
26336 KB |
Output is correct |
18 |
Correct |
6 ms |
26204 KB |
Output is correct |
19 |
Correct |
6 ms |
26344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
26200 KB |
Output is correct |
2 |
Correct |
8 ms |
26204 KB |
Output is correct |
3 |
Correct |
6 ms |
26244 KB |
Output is correct |
4 |
Correct |
6 ms |
26204 KB |
Output is correct |
5 |
Correct |
7 ms |
26236 KB |
Output is correct |
6 |
Correct |
6 ms |
26204 KB |
Output is correct |
7 |
Correct |
6 ms |
26292 KB |
Output is correct |
8 |
Correct |
6 ms |
26204 KB |
Output is correct |
9 |
Correct |
6 ms |
26204 KB |
Output is correct |
10 |
Correct |
6 ms |
26204 KB |
Output is correct |
11 |
Correct |
6 ms |
26204 KB |
Output is correct |
12 |
Correct |
7 ms |
26204 KB |
Output is correct |
13 |
Correct |
6 ms |
26204 KB |
Output is correct |
14 |
Correct |
6 ms |
26204 KB |
Output is correct |
15 |
Correct |
6 ms |
26228 KB |
Output is correct |
16 |
Correct |
6 ms |
26200 KB |
Output is correct |
17 |
Correct |
6 ms |
26204 KB |
Output is correct |
18 |
Correct |
6 ms |
26200 KB |
Output is correct |
19 |
Correct |
6 ms |
26204 KB |
Output is correct |
20 |
Correct |
6 ms |
26204 KB |
Output is correct |
21 |
Correct |
6 ms |
26200 KB |
Output is correct |
22 |
Correct |
6 ms |
26456 KB |
Output is correct |
23 |
Correct |
6 ms |
26204 KB |
Output is correct |
24 |
Correct |
6 ms |
26204 KB |
Output is correct |
25 |
Correct |
7 ms |
26204 KB |
Output is correct |
26 |
Correct |
6 ms |
26204 KB |
Output is correct |
27 |
Correct |
6 ms |
26336 KB |
Output is correct |
28 |
Correct |
6 ms |
26204 KB |
Output is correct |
29 |
Correct |
6 ms |
26344 KB |
Output is correct |
30 |
Correct |
6 ms |
26204 KB |
Output is correct |
31 |
Correct |
7 ms |
26460 KB |
Output is correct |
32 |
Correct |
9 ms |
26516 KB |
Output is correct |
33 |
Correct |
8 ms |
26460 KB |
Output is correct |
34 |
Correct |
8 ms |
26500 KB |
Output is correct |
35 |
Correct |
10 ms |
26716 KB |
Output is correct |
36 |
Correct |
9 ms |
26716 KB |
Output is correct |
37 |
Correct |
9 ms |
27224 KB |
Output is correct |
38 |
Correct |
9 ms |
26972 KB |
Output is correct |
39 |
Correct |
9 ms |
26972 KB |
Output is correct |
40 |
Correct |
8 ms |
27740 KB |
Output is correct |
41 |
Correct |
8 ms |
27740 KB |
Output is correct |
42 |
Correct |
7 ms |
26456 KB |
Output is correct |
43 |
Correct |
9 ms |
27652 KB |
Output is correct |
44 |
Correct |
9 ms |
27312 KB |
Output is correct |
45 |
Correct |
9 ms |
27480 KB |
Output is correct |
46 |
Correct |
11 ms |
27480 KB |
Output is correct |
47 |
Correct |
10 ms |
27484 KB |
Output is correct |
48 |
Correct |
10 ms |
27340 KB |
Output is correct |
49 |
Correct |
9 ms |
27484 KB |
Output is correct |
50 |
Correct |
9 ms |
27248 KB |
Output is correct |
51 |
Correct |
9 ms |
27224 KB |
Output is correct |
52 |
Correct |
9 ms |
27228 KB |
Output is correct |
53 |
Correct |
9 ms |
27484 KB |
Output is correct |
54 |
Correct |
9 ms |
27228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
26200 KB |
Output is correct |
2 |
Correct |
8 ms |
26204 KB |
Output is correct |
3 |
Correct |
6 ms |
26244 KB |
Output is correct |
4 |
Correct |
6 ms |
26204 KB |
Output is correct |
5 |
Correct |
7 ms |
26236 KB |
Output is correct |
6 |
Correct |
6 ms |
26204 KB |
Output is correct |
7 |
Correct |
6 ms |
26292 KB |
Output is correct |
8 |
Correct |
6 ms |
26204 KB |
Output is correct |
9 |
Correct |
6 ms |
26204 KB |
Output is correct |
10 |
Correct |
6 ms |
26204 KB |
Output is correct |
11 |
Correct |
6 ms |
26204 KB |
Output is correct |
12 |
Correct |
7 ms |
26204 KB |
Output is correct |
13 |
Correct |
6 ms |
26204 KB |
Output is correct |
14 |
Correct |
6 ms |
26204 KB |
Output is correct |
15 |
Correct |
6 ms |
26228 KB |
Output is correct |
16 |
Correct |
6 ms |
26200 KB |
Output is correct |
17 |
Correct |
6 ms |
26204 KB |
Output is correct |
18 |
Correct |
6 ms |
26200 KB |
Output is correct |
19 |
Correct |
6 ms |
26204 KB |
Output is correct |
20 |
Correct |
6 ms |
26204 KB |
Output is correct |
21 |
Correct |
6 ms |
26200 KB |
Output is correct |
22 |
Correct |
6 ms |
26456 KB |
Output is correct |
23 |
Correct |
6 ms |
26204 KB |
Output is correct |
24 |
Correct |
6 ms |
26204 KB |
Output is correct |
25 |
Correct |
7 ms |
26204 KB |
Output is correct |
26 |
Correct |
6 ms |
26204 KB |
Output is correct |
27 |
Correct |
6 ms |
26336 KB |
Output is correct |
28 |
Correct |
6 ms |
26204 KB |
Output is correct |
29 |
Correct |
6 ms |
26344 KB |
Output is correct |
30 |
Correct |
6 ms |
26204 KB |
Output is correct |
31 |
Correct |
7 ms |
26460 KB |
Output is correct |
32 |
Correct |
9 ms |
26516 KB |
Output is correct |
33 |
Correct |
8 ms |
26460 KB |
Output is correct |
34 |
Correct |
8 ms |
26500 KB |
Output is correct |
35 |
Correct |
10 ms |
26716 KB |
Output is correct |
36 |
Correct |
9 ms |
26716 KB |
Output is correct |
37 |
Correct |
9 ms |
27224 KB |
Output is correct |
38 |
Correct |
9 ms |
26972 KB |
Output is correct |
39 |
Correct |
9 ms |
26972 KB |
Output is correct |
40 |
Correct |
8 ms |
27740 KB |
Output is correct |
41 |
Correct |
8 ms |
27740 KB |
Output is correct |
42 |
Correct |
7 ms |
26456 KB |
Output is correct |
43 |
Correct |
9 ms |
27652 KB |
Output is correct |
44 |
Correct |
9 ms |
27312 KB |
Output is correct |
45 |
Correct |
9 ms |
27480 KB |
Output is correct |
46 |
Correct |
11 ms |
27480 KB |
Output is correct |
47 |
Correct |
10 ms |
27484 KB |
Output is correct |
48 |
Correct |
10 ms |
27340 KB |
Output is correct |
49 |
Correct |
9 ms |
27484 KB |
Output is correct |
50 |
Correct |
9 ms |
27248 KB |
Output is correct |
51 |
Correct |
9 ms |
27224 KB |
Output is correct |
52 |
Correct |
9 ms |
27228 KB |
Output is correct |
53 |
Correct |
9 ms |
27484 KB |
Output is correct |
54 |
Correct |
9 ms |
27228 KB |
Output is correct |
55 |
Correct |
14 ms |
28252 KB |
Output is correct |
56 |
Correct |
41 ms |
34396 KB |
Output is correct |
57 |
Correct |
71 ms |
40276 KB |
Output is correct |
58 |
Correct |
108 ms |
44604 KB |
Output is correct |
59 |
Correct |
138 ms |
50620 KB |
Output is correct |
60 |
Correct |
177 ms |
57072 KB |
Output is correct |
61 |
Correct |
213 ms |
61008 KB |
Output is correct |
62 |
Correct |
225 ms |
65064 KB |
Output is correct |
63 |
Correct |
286 ms |
73804 KB |
Output is correct |
64 |
Correct |
313 ms |
74520 KB |
Output is correct |
65 |
Correct |
137 ms |
120400 KB |
Output is correct |
66 |
Correct |
143 ms |
120240 KB |
Output is correct |
67 |
Correct |
113 ms |
40580 KB |
Output is correct |
68 |
Correct |
241 ms |
110672 KB |
Output is correct |
69 |
Correct |
303 ms |
109408 KB |
Output is correct |
70 |
Correct |
293 ms |
109224 KB |
Output is correct |
71 |
Correct |
328 ms |
129816 KB |
Output is correct |
72 |
Correct |
319 ms |
129108 KB |
Output is correct |
73 |
Correct |
345 ms |
115596 KB |
Output is correct |
74 |
Correct |
330 ms |
115952 KB |
Output is correct |
75 |
Correct |
419 ms |
114520 KB |
Output is correct |
76 |
Correct |
391 ms |
114512 KB |
Output is correct |
77 |
Correct |
393 ms |
111528 KB |
Output is correct |
78 |
Correct |
459 ms |
109152 KB |
Output is correct |
79 |
Correct |
403 ms |
100156 KB |
Output is correct |