#include <bits/stdc++.h>
#include <utility>
using namespace std;
#define int long long
struct line{
int m,n;
multiset<int> points;
line(){}
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;
line curr(0,0,{});
for(auto&i:adj[x]){
if(i.first!=p){
dfs(i.first,x);
if(functions[i.first].points.size()>curr.points.size())swap(curr,functions[i.first]);
curr+=functions[i.first];
}
else len = i.second;
}
int currsum = curr.n;
int currslope = curr.m;
for(auto i=--curr.points.end();i!=curr.points.begin();i--){
currslope--;
if(currslope==-1)break;
currsum+=*i;
}
while(curr.m>1){
curr.m--;
curr.points.erase(--curr.points.end());
}
int f = *--curr.points.end();
curr.points.erase(--curr.points.end());
int s = *--curr.points.end();
curr.points.erase(--curr.points.end());
curr.points.insert(s+len);
curr.points.insert(f+len);
curr.n = currsum-(f+len);
functions[x] = curr;
}
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';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26204 KB |
Output is correct |
2 |
Correct |
6 ms |
26200 KB |
Output is correct |
3 |
Correct |
6 ms |
26204 KB |
Output is correct |
4 |
Correct |
6 ms |
26260 KB |
Output is correct |
5 |
Correct |
6 ms |
26204 KB |
Output is correct |
6 |
Correct |
6 ms |
26204 KB |
Output is correct |
7 |
Correct |
6 ms |
26204 KB |
Output is correct |
8 |
Correct |
6 ms |
26204 KB |
Output is correct |
9 |
Correct |
6 ms |
26460 KB |
Output is correct |
10 |
Correct |
7 ms |
26204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26204 KB |
Output is correct |
2 |
Correct |
8 ms |
26268 KB |
Output is correct |
3 |
Correct |
7 ms |
26456 KB |
Output is correct |
4 |
Correct |
6 ms |
26200 KB |
Output is correct |
5 |
Correct |
6 ms |
26200 KB |
Output is correct |
6 |
Correct |
6 ms |
26204 KB |
Output is correct |
7 |
Correct |
5 ms |
26204 KB |
Output is correct |
8 |
Correct |
6 ms |
26320 KB |
Output is correct |
9 |
Correct |
6 ms |
26200 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 |
26204 KB |
Output is correct |
13 |
Correct |
6 ms |
26456 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 |
26204 KB |
Output is correct |
18 |
Correct |
6 ms |
26204 KB |
Output is correct |
19 |
Correct |
7 ms |
26204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26204 KB |
Output is correct |
2 |
Correct |
6 ms |
26200 KB |
Output is correct |
3 |
Correct |
6 ms |
26204 KB |
Output is correct |
4 |
Correct |
6 ms |
26260 KB |
Output is correct |
5 |
Correct |
6 ms |
26204 KB |
Output is correct |
6 |
Correct |
6 ms |
26204 KB |
Output is correct |
7 |
Correct |
6 ms |
26204 KB |
Output is correct |
8 |
Correct |
6 ms |
26204 KB |
Output is correct |
9 |
Correct |
6 ms |
26460 KB |
Output is correct |
10 |
Correct |
7 ms |
26204 KB |
Output is correct |
11 |
Correct |
6 ms |
26204 KB |
Output is correct |
12 |
Correct |
8 ms |
26268 KB |
Output is correct |
13 |
Correct |
7 ms |
26456 KB |
Output is correct |
14 |
Correct |
6 ms |
26200 KB |
Output is correct |
15 |
Correct |
6 ms |
26200 KB |
Output is correct |
16 |
Correct |
6 ms |
26204 KB |
Output is correct |
17 |
Correct |
5 ms |
26204 KB |
Output is correct |
18 |
Correct |
6 ms |
26320 KB |
Output is correct |
19 |
Correct |
6 ms |
26200 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 |
26204 KB |
Output is correct |
23 |
Correct |
6 ms |
26456 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 |
26204 KB |
Output is correct |
28 |
Correct |
6 ms |
26204 KB |
Output is correct |
29 |
Correct |
7 ms |
26204 KB |
Output is correct |
30 |
Correct |
7 ms |
26200 KB |
Output is correct |
31 |
Correct |
7 ms |
26460 KB |
Output is correct |
32 |
Correct |
8 ms |
26448 KB |
Output is correct |
33 |
Correct |
8 ms |
26460 KB |
Output is correct |
34 |
Correct |
9 ms |
26716 KB |
Output is correct |
35 |
Correct |
10 ms |
26744 KB |
Output is correct |
36 |
Correct |
14 ms |
26716 KB |
Output is correct |
37 |
Correct |
12 ms |
27384 KB |
Output is correct |
38 |
Correct |
15 ms |
26968 KB |
Output is correct |
39 |
Correct |
13 ms |
27224 KB |
Output is correct |
40 |
Correct |
8 ms |
27740 KB |
Output is correct |
41 |
Correct |
8 ms |
27740 KB |
Output is correct |
42 |
Correct |
8 ms |
26576 KB |
Output is correct |
43 |
Correct |
87 ms |
27708 KB |
Output is correct |
44 |
Correct |
68 ms |
27396 KB |
Output is correct |
45 |
Correct |
69 ms |
27472 KB |
Output is correct |
46 |
Correct |
10 ms |
27484 KB |
Output is correct |
47 |
Correct |
9 ms |
27528 KB |
Output is correct |
48 |
Correct |
9 ms |
27480 KB |
Output is correct |
49 |
Correct |
9 ms |
27484 KB |
Output is correct |
50 |
Correct |
9 ms |
27256 KB |
Output is correct |
51 |
Correct |
9 ms |
27228 KB |
Output is correct |
52 |
Correct |
11 ms |
27228 KB |
Output is correct |
53 |
Correct |
10 ms |
27484 KB |
Output is correct |
54 |
Correct |
9 ms |
27480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26204 KB |
Output is correct |
2 |
Correct |
6 ms |
26200 KB |
Output is correct |
3 |
Correct |
6 ms |
26204 KB |
Output is correct |
4 |
Correct |
6 ms |
26260 KB |
Output is correct |
5 |
Correct |
6 ms |
26204 KB |
Output is correct |
6 |
Correct |
6 ms |
26204 KB |
Output is correct |
7 |
Correct |
6 ms |
26204 KB |
Output is correct |
8 |
Correct |
6 ms |
26204 KB |
Output is correct |
9 |
Correct |
6 ms |
26460 KB |
Output is correct |
10 |
Correct |
7 ms |
26204 KB |
Output is correct |
11 |
Correct |
6 ms |
26204 KB |
Output is correct |
12 |
Correct |
8 ms |
26268 KB |
Output is correct |
13 |
Correct |
7 ms |
26456 KB |
Output is correct |
14 |
Correct |
6 ms |
26200 KB |
Output is correct |
15 |
Correct |
6 ms |
26200 KB |
Output is correct |
16 |
Correct |
6 ms |
26204 KB |
Output is correct |
17 |
Correct |
5 ms |
26204 KB |
Output is correct |
18 |
Correct |
6 ms |
26320 KB |
Output is correct |
19 |
Correct |
6 ms |
26200 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 |
26204 KB |
Output is correct |
23 |
Correct |
6 ms |
26456 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 |
26204 KB |
Output is correct |
28 |
Correct |
6 ms |
26204 KB |
Output is correct |
29 |
Correct |
7 ms |
26204 KB |
Output is correct |
30 |
Correct |
7 ms |
26200 KB |
Output is correct |
31 |
Correct |
7 ms |
26460 KB |
Output is correct |
32 |
Correct |
8 ms |
26448 KB |
Output is correct |
33 |
Correct |
8 ms |
26460 KB |
Output is correct |
34 |
Correct |
9 ms |
26716 KB |
Output is correct |
35 |
Correct |
10 ms |
26744 KB |
Output is correct |
36 |
Correct |
14 ms |
26716 KB |
Output is correct |
37 |
Correct |
12 ms |
27384 KB |
Output is correct |
38 |
Correct |
15 ms |
26968 KB |
Output is correct |
39 |
Correct |
13 ms |
27224 KB |
Output is correct |
40 |
Correct |
8 ms |
27740 KB |
Output is correct |
41 |
Correct |
8 ms |
27740 KB |
Output is correct |
42 |
Correct |
8 ms |
26576 KB |
Output is correct |
43 |
Correct |
87 ms |
27708 KB |
Output is correct |
44 |
Correct |
68 ms |
27396 KB |
Output is correct |
45 |
Correct |
69 ms |
27472 KB |
Output is correct |
46 |
Correct |
10 ms |
27484 KB |
Output is correct |
47 |
Correct |
9 ms |
27528 KB |
Output is correct |
48 |
Correct |
9 ms |
27480 KB |
Output is correct |
49 |
Correct |
9 ms |
27484 KB |
Output is correct |
50 |
Correct |
9 ms |
27256 KB |
Output is correct |
51 |
Correct |
9 ms |
27228 KB |
Output is correct |
52 |
Correct |
11 ms |
27228 KB |
Output is correct |
53 |
Correct |
10 ms |
27484 KB |
Output is correct |
54 |
Correct |
9 ms |
27480 KB |
Output is correct |
55 |
Correct |
30 ms |
28244 KB |
Output is correct |
56 |
Correct |
207 ms |
34384 KB |
Output is correct |
57 |
Correct |
434 ms |
40392 KB |
Output is correct |
58 |
Correct |
505 ms |
44536 KB |
Output is correct |
59 |
Correct |
1225 ms |
50612 KB |
Output is correct |
60 |
Correct |
1214 ms |
56744 KB |
Output is correct |
61 |
Correct |
1707 ms |
61264 KB |
Output is correct |
62 |
Correct |
1797 ms |
65164 KB |
Output is correct |
63 |
Correct |
1798 ms |
73408 KB |
Output is correct |
64 |
Execution timed out |
2073 ms |
69828 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |