#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
vector<pii> g[300005];
multiset<ll> pts[300005];
void dfs(int x,int w){
for(auto&p:g[x]) dfs(p.fi,p.se);
int ch=-1;
for(auto&p:g[x]) if(ch==-1||(int)pts[ch].size()<(int)pts[p.fi].size()) ch=p.fi;
if(ch!=-1) swap(pts[x],pts[ch]);
for(auto&p:g[x]) if(p.fi!=ch) for(ll pt:pts[p.fi]) pts[x].insert(pt);
for(int i=0;i<(int)g[x].size()-1;i++) pts[x].erase(prev(pts[x].end()));
if(w==-1) return;
if(pts[x].empty()){
pts[x].insert(w);
pts[x].insert(w);
} else {
ll a=*prev(pts[x].end());
pts[x].erase(prev(pts[x].end()));
ll b=*prev(pts[x].end());
pts[x].erase(prev(pts[x].end()));
pts[x].insert(a+w);
pts[x].insert(b+w);
}
}
int main(){
n=readint(); m=readint();
n+=m;
for(int i=2;i<=n;i++){
int p=readint(),c=readint();
g[p].pb(mp(i,c));
}
ll tot=0;
for(int i=1;i<=n;i++) for(auto&p:g[i]) tot+=p.se;
dfs(1,-1);
vector<ll> vec(1,0);
for(ll pt:pts[1]) vec.pb(pt);
int c=0;
for(int i=vec.size()-1;i>0;i--){
tot+=c*(vec[i]-vec[i-1]);
c--;
}
printf("%lld\n",tot);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21340 KB |
Output is correct |
2 |
Correct |
6 ms |
21340 KB |
Output is correct |
3 |
Correct |
4 ms |
21596 KB |
Output is correct |
4 |
Correct |
4 ms |
21596 KB |
Output is correct |
5 |
Correct |
7 ms |
21336 KB |
Output is correct |
6 |
Correct |
4 ms |
21340 KB |
Output is correct |
7 |
Correct |
4 ms |
21596 KB |
Output is correct |
8 |
Correct |
5 ms |
21596 KB |
Output is correct |
9 |
Correct |
5 ms |
21596 KB |
Output is correct |
10 |
Correct |
4 ms |
21596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21340 KB |
Output is correct |
2 |
Correct |
5 ms |
21544 KB |
Output is correct |
3 |
Correct |
4 ms |
21340 KB |
Output is correct |
4 |
Correct |
4 ms |
21340 KB |
Output is correct |
5 |
Correct |
5 ms |
21340 KB |
Output is correct |
6 |
Correct |
4 ms |
21340 KB |
Output is correct |
7 |
Correct |
4 ms |
21336 KB |
Output is correct |
8 |
Correct |
4 ms |
21596 KB |
Output is correct |
9 |
Correct |
6 ms |
21336 KB |
Output is correct |
10 |
Correct |
5 ms |
21444 KB |
Output is correct |
11 |
Correct |
5 ms |
21596 KB |
Output is correct |
12 |
Correct |
5 ms |
21612 KB |
Output is correct |
13 |
Correct |
5 ms |
21596 KB |
Output is correct |
14 |
Correct |
4 ms |
21592 KB |
Output is correct |
15 |
Correct |
5 ms |
21592 KB |
Output is correct |
16 |
Correct |
4 ms |
21596 KB |
Output is correct |
17 |
Correct |
4 ms |
21592 KB |
Output is correct |
18 |
Correct |
4 ms |
21596 KB |
Output is correct |
19 |
Correct |
4 ms |
21596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21340 KB |
Output is correct |
2 |
Correct |
6 ms |
21340 KB |
Output is correct |
3 |
Correct |
4 ms |
21596 KB |
Output is correct |
4 |
Correct |
4 ms |
21596 KB |
Output is correct |
5 |
Correct |
7 ms |
21336 KB |
Output is correct |
6 |
Correct |
4 ms |
21340 KB |
Output is correct |
7 |
Correct |
4 ms |
21596 KB |
Output is correct |
8 |
Correct |
5 ms |
21596 KB |
Output is correct |
9 |
Correct |
5 ms |
21596 KB |
Output is correct |
10 |
Correct |
4 ms |
21596 KB |
Output is correct |
11 |
Correct |
4 ms |
21340 KB |
Output is correct |
12 |
Correct |
5 ms |
21544 KB |
Output is correct |
13 |
Correct |
4 ms |
21340 KB |
Output is correct |
14 |
Correct |
4 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21340 KB |
Output is correct |
16 |
Correct |
4 ms |
21340 KB |
Output is correct |
17 |
Correct |
4 ms |
21336 KB |
Output is correct |
18 |
Correct |
4 ms |
21596 KB |
Output is correct |
19 |
Correct |
6 ms |
21336 KB |
Output is correct |
20 |
Correct |
5 ms |
21444 KB |
Output is correct |
21 |
Correct |
5 ms |
21596 KB |
Output is correct |
22 |
Correct |
5 ms |
21612 KB |
Output is correct |
23 |
Correct |
5 ms |
21596 KB |
Output is correct |
24 |
Correct |
4 ms |
21592 KB |
Output is correct |
25 |
Correct |
5 ms |
21592 KB |
Output is correct |
26 |
Correct |
4 ms |
21596 KB |
Output is correct |
27 |
Correct |
4 ms |
21592 KB |
Output is correct |
28 |
Correct |
4 ms |
21596 KB |
Output is correct |
29 |
Correct |
4 ms |
21596 KB |
Output is correct |
30 |
Correct |
4 ms |
21596 KB |
Output is correct |
31 |
Correct |
5 ms |
21596 KB |
Output is correct |
32 |
Correct |
6 ms |
21584 KB |
Output is correct |
33 |
Correct |
5 ms |
21844 KB |
Output is correct |
34 |
Correct |
6 ms |
21848 KB |
Output is correct |
35 |
Correct |
6 ms |
21852 KB |
Output is correct |
36 |
Correct |
6 ms |
21852 KB |
Output is correct |
37 |
Correct |
6 ms |
22108 KB |
Output is correct |
38 |
Correct |
7 ms |
22108 KB |
Output is correct |
39 |
Correct |
6 ms |
22108 KB |
Output is correct |
40 |
Correct |
5 ms |
22200 KB |
Output is correct |
41 |
Correct |
6 ms |
22364 KB |
Output is correct |
42 |
Correct |
6 ms |
21596 KB |
Output is correct |
43 |
Correct |
6 ms |
22360 KB |
Output is correct |
44 |
Correct |
7 ms |
22364 KB |
Output is correct |
45 |
Correct |
7 ms |
22364 KB |
Output is correct |
46 |
Correct |
7 ms |
22620 KB |
Output is correct |
47 |
Correct |
7 ms |
22620 KB |
Output is correct |
48 |
Correct |
6 ms |
22360 KB |
Output is correct |
49 |
Correct |
8 ms |
22620 KB |
Output is correct |
50 |
Correct |
7 ms |
22364 KB |
Output is correct |
51 |
Correct |
7 ms |
22364 KB |
Output is correct |
52 |
Correct |
6 ms |
22428 KB |
Output is correct |
53 |
Correct |
7 ms |
22352 KB |
Output is correct |
54 |
Correct |
7 ms |
22620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21340 KB |
Output is correct |
2 |
Correct |
6 ms |
21340 KB |
Output is correct |
3 |
Correct |
4 ms |
21596 KB |
Output is correct |
4 |
Correct |
4 ms |
21596 KB |
Output is correct |
5 |
Correct |
7 ms |
21336 KB |
Output is correct |
6 |
Correct |
4 ms |
21340 KB |
Output is correct |
7 |
Correct |
4 ms |
21596 KB |
Output is correct |
8 |
Correct |
5 ms |
21596 KB |
Output is correct |
9 |
Correct |
5 ms |
21596 KB |
Output is correct |
10 |
Correct |
4 ms |
21596 KB |
Output is correct |
11 |
Correct |
4 ms |
21340 KB |
Output is correct |
12 |
Correct |
5 ms |
21544 KB |
Output is correct |
13 |
Correct |
4 ms |
21340 KB |
Output is correct |
14 |
Correct |
4 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21340 KB |
Output is correct |
16 |
Correct |
4 ms |
21340 KB |
Output is correct |
17 |
Correct |
4 ms |
21336 KB |
Output is correct |
18 |
Correct |
4 ms |
21596 KB |
Output is correct |
19 |
Correct |
6 ms |
21336 KB |
Output is correct |
20 |
Correct |
5 ms |
21444 KB |
Output is correct |
21 |
Correct |
5 ms |
21596 KB |
Output is correct |
22 |
Correct |
5 ms |
21612 KB |
Output is correct |
23 |
Correct |
5 ms |
21596 KB |
Output is correct |
24 |
Correct |
4 ms |
21592 KB |
Output is correct |
25 |
Correct |
5 ms |
21592 KB |
Output is correct |
26 |
Correct |
4 ms |
21596 KB |
Output is correct |
27 |
Correct |
4 ms |
21592 KB |
Output is correct |
28 |
Correct |
4 ms |
21596 KB |
Output is correct |
29 |
Correct |
4 ms |
21596 KB |
Output is correct |
30 |
Correct |
4 ms |
21596 KB |
Output is correct |
31 |
Correct |
5 ms |
21596 KB |
Output is correct |
32 |
Correct |
6 ms |
21584 KB |
Output is correct |
33 |
Correct |
5 ms |
21844 KB |
Output is correct |
34 |
Correct |
6 ms |
21848 KB |
Output is correct |
35 |
Correct |
6 ms |
21852 KB |
Output is correct |
36 |
Correct |
6 ms |
21852 KB |
Output is correct |
37 |
Correct |
6 ms |
22108 KB |
Output is correct |
38 |
Correct |
7 ms |
22108 KB |
Output is correct |
39 |
Correct |
6 ms |
22108 KB |
Output is correct |
40 |
Correct |
5 ms |
22200 KB |
Output is correct |
41 |
Correct |
6 ms |
22364 KB |
Output is correct |
42 |
Correct |
6 ms |
21596 KB |
Output is correct |
43 |
Correct |
6 ms |
22360 KB |
Output is correct |
44 |
Correct |
7 ms |
22364 KB |
Output is correct |
45 |
Correct |
7 ms |
22364 KB |
Output is correct |
46 |
Correct |
7 ms |
22620 KB |
Output is correct |
47 |
Correct |
7 ms |
22620 KB |
Output is correct |
48 |
Correct |
6 ms |
22360 KB |
Output is correct |
49 |
Correct |
8 ms |
22620 KB |
Output is correct |
50 |
Correct |
7 ms |
22364 KB |
Output is correct |
51 |
Correct |
7 ms |
22364 KB |
Output is correct |
52 |
Correct |
6 ms |
22428 KB |
Output is correct |
53 |
Correct |
7 ms |
22352 KB |
Output is correct |
54 |
Correct |
7 ms |
22620 KB |
Output is correct |
55 |
Correct |
10 ms |
23128 KB |
Output is correct |
56 |
Correct |
29 ms |
28348 KB |
Output is correct |
57 |
Correct |
46 ms |
33368 KB |
Output is correct |
58 |
Correct |
64 ms |
36996 KB |
Output is correct |
59 |
Correct |
83 ms |
41860 KB |
Output is correct |
60 |
Correct |
108 ms |
47060 KB |
Output is correct |
61 |
Correct |
139 ms |
51144 KB |
Output is correct |
62 |
Correct |
176 ms |
54364 KB |
Output is correct |
63 |
Correct |
193 ms |
61392 KB |
Output is correct |
64 |
Correct |
206 ms |
61856 KB |
Output is correct |
65 |
Correct |
78 ms |
73348 KB |
Output is correct |
66 |
Correct |
78 ms |
73296 KB |
Output is correct |
67 |
Correct |
58 ms |
35924 KB |
Output is correct |
68 |
Correct |
148 ms |
72896 KB |
Output is correct |
69 |
Correct |
193 ms |
70908 KB |
Output is correct |
70 |
Correct |
223 ms |
70860 KB |
Output is correct |
71 |
Correct |
242 ms |
102536 KB |
Output is correct |
72 |
Correct |
251 ms |
103292 KB |
Output is correct |
73 |
Correct |
250 ms |
89180 KB |
Output is correct |
74 |
Correct |
259 ms |
89724 KB |
Output is correct |
75 |
Correct |
232 ms |
89136 KB |
Output is correct |
76 |
Correct |
244 ms |
89032 KB |
Output is correct |
77 |
Correct |
250 ms |
88004 KB |
Output is correct |
78 |
Correct |
269 ms |
85472 KB |
Output is correct |
79 |
Correct |
275 ms |
90296 KB |
Output is correct |