#include <bits/stdc++.h>
#include <utility>
using namespace std;
#define int long long
struct line{
int m,n;
vector<int> points;
line(int m,int n,vector<int> points):m(m),n(n),points(points){}
void operator+=(const line &other){
m+=other.m;
n+=other.n;
vector<int> newpoints(points.size()+other.points.size());
merge(points.begin(), points.end(),other.points.begin(), other.points.end(),newpoints.begin());
points = newpoints;
}
};
int n,m;
vector<pair<int,int>> adj[300001];
line dfs(int x,int p){
if(x>n)return {1,-adj[x][0].second,{adj[x][0].second,adj[x][0].second}};
int len = 0;
line curr(0,0,{});
for(auto&i:adj[x]){
if(i.first!=p)curr+=dfs(i.first,x);
else len = i.second;
}
int currsum = curr.n;
int currslope = curr.m;
for(auto i=curr.points.rbegin();i!=curr.points.rend();i++){
currslope--;
if(currslope==-1)break;
currsum+=*i;
}
while(curr.m>1){
curr.m--;
curr.points.erase(--curr.points.end());
}
curr.points[curr.points.size()-1]+=len;
curr.points[curr.points.size()-2]+=len;
curr.n = currsum-curr.points.back();
return 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);
}
auto ans = dfs(1,0);
int currsum = ans.n;
int currslope = ans.m;
for(auto i=ans.points.rbegin();i!=ans.points.rend();i++){
currslope--;
if(currslope==-1)break;
currsum+=*i;
}
cout << currsum << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7500 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7260 KB |
Output is correct |
2 |
Correct |
3 ms |
7436 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7752 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
2 ms |
7520 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
3 ms |
7528 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7516 KB |
Output is correct |
18 |
Correct |
2 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7500 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7260 KB |
Output is correct |
12 |
Correct |
3 ms |
7436 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7516 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7752 KB |
Output is correct |
18 |
Correct |
2 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Correct |
2 ms |
7516 KB |
Output is correct |
22 |
Correct |
2 ms |
7520 KB |
Output is correct |
23 |
Correct |
2 ms |
7516 KB |
Output is correct |
24 |
Correct |
3 ms |
7528 KB |
Output is correct |
25 |
Correct |
2 ms |
7516 KB |
Output is correct |
26 |
Correct |
2 ms |
7516 KB |
Output is correct |
27 |
Correct |
2 ms |
7516 KB |
Output is correct |
28 |
Correct |
2 ms |
7516 KB |
Output is correct |
29 |
Correct |
2 ms |
7516 KB |
Output is correct |
30 |
Correct |
2 ms |
7516 KB |
Output is correct |
31 |
Correct |
2 ms |
7512 KB |
Output is correct |
32 |
Correct |
2 ms |
7512 KB |
Output is correct |
33 |
Correct |
3 ms |
7772 KB |
Output is correct |
34 |
Correct |
4 ms |
7772 KB |
Output is correct |
35 |
Correct |
4 ms |
7772 KB |
Output is correct |
36 |
Correct |
3 ms |
7768 KB |
Output is correct |
37 |
Correct |
3 ms |
7776 KB |
Output is correct |
38 |
Correct |
5 ms |
7772 KB |
Output is correct |
39 |
Correct |
5 ms |
7772 KB |
Output is correct |
40 |
Correct |
4 ms |
9052 KB |
Output is correct |
41 |
Correct |
4 ms |
9048 KB |
Output is correct |
42 |
Correct |
3 ms |
7772 KB |
Output is correct |
43 |
Correct |
7 ms |
8540 KB |
Output is correct |
44 |
Correct |
7 ms |
8284 KB |
Output is correct |
45 |
Correct |
7 ms |
8444 KB |
Output is correct |
46 |
Correct |
4 ms |
8028 KB |
Output is correct |
47 |
Correct |
5 ms |
8028 KB |
Output is correct |
48 |
Correct |
8 ms |
7884 KB |
Output is correct |
49 |
Correct |
9 ms |
8028 KB |
Output is correct |
50 |
Correct |
14 ms |
8028 KB |
Output is correct |
51 |
Correct |
13 ms |
7772 KB |
Output is correct |
52 |
Correct |
15 ms |
8028 KB |
Output is correct |
53 |
Correct |
10 ms |
7772 KB |
Output is correct |
54 |
Correct |
24 ms |
8168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7500 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
3 ms |
7260 KB |
Output is correct |
12 |
Correct |
3 ms |
7436 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7516 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7752 KB |
Output is correct |
18 |
Correct |
2 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Correct |
2 ms |
7516 KB |
Output is correct |
22 |
Correct |
2 ms |
7520 KB |
Output is correct |
23 |
Correct |
2 ms |
7516 KB |
Output is correct |
24 |
Correct |
3 ms |
7528 KB |
Output is correct |
25 |
Correct |
2 ms |
7516 KB |
Output is correct |
26 |
Correct |
2 ms |
7516 KB |
Output is correct |
27 |
Correct |
2 ms |
7516 KB |
Output is correct |
28 |
Correct |
2 ms |
7516 KB |
Output is correct |
29 |
Correct |
2 ms |
7516 KB |
Output is correct |
30 |
Correct |
2 ms |
7516 KB |
Output is correct |
31 |
Correct |
2 ms |
7512 KB |
Output is correct |
32 |
Correct |
2 ms |
7512 KB |
Output is correct |
33 |
Correct |
3 ms |
7772 KB |
Output is correct |
34 |
Correct |
4 ms |
7772 KB |
Output is correct |
35 |
Correct |
4 ms |
7772 KB |
Output is correct |
36 |
Correct |
3 ms |
7768 KB |
Output is correct |
37 |
Correct |
3 ms |
7776 KB |
Output is correct |
38 |
Correct |
5 ms |
7772 KB |
Output is correct |
39 |
Correct |
5 ms |
7772 KB |
Output is correct |
40 |
Correct |
4 ms |
9052 KB |
Output is correct |
41 |
Correct |
4 ms |
9048 KB |
Output is correct |
42 |
Correct |
3 ms |
7772 KB |
Output is correct |
43 |
Correct |
7 ms |
8540 KB |
Output is correct |
44 |
Correct |
7 ms |
8284 KB |
Output is correct |
45 |
Correct |
7 ms |
8444 KB |
Output is correct |
46 |
Correct |
4 ms |
8028 KB |
Output is correct |
47 |
Correct |
5 ms |
8028 KB |
Output is correct |
48 |
Correct |
8 ms |
7884 KB |
Output is correct |
49 |
Correct |
9 ms |
8028 KB |
Output is correct |
50 |
Correct |
14 ms |
8028 KB |
Output is correct |
51 |
Correct |
13 ms |
7772 KB |
Output is correct |
52 |
Correct |
15 ms |
8028 KB |
Output is correct |
53 |
Correct |
10 ms |
7772 KB |
Output is correct |
54 |
Correct |
24 ms |
8168 KB |
Output is correct |
55 |
Correct |
7 ms |
8540 KB |
Output is correct |
56 |
Correct |
34 ms |
11588 KB |
Output is correct |
57 |
Correct |
70 ms |
14360 KB |
Output is correct |
58 |
Correct |
72 ms |
16508 KB |
Output is correct |
59 |
Correct |
158 ms |
19036 KB |
Output is correct |
60 |
Correct |
157 ms |
21776 KB |
Output is correct |
61 |
Correct |
268 ms |
23724 KB |
Output is correct |
62 |
Correct |
233 ms |
25704 KB |
Output is correct |
63 |
Correct |
248 ms |
28712 KB |
Output is correct |
64 |
Correct |
385 ms |
29944 KB |
Output is correct |
65 |
Correct |
123 ms |
106108 KB |
Output is correct |
66 |
Correct |
129 ms |
106280 KB |
Output is correct |
67 |
Correct |
103 ms |
26700 KB |
Output is correct |
68 |
Execution timed out |
2052 ms |
71352 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |