#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;
const ll maxn=3e5+10;
vector<pii> g[maxn];
multiset<ll> slope[maxn];
void dfs(ll node,ll c=-1){
for(auto i:g[node]){
dfs(i.first,i.second);
}
pair<ll,ll> maxi={-1,-1};
for(auto i:g[node]){
maxi=max(maxi,{ll(slope[i.first].size()),i.first});
}
if(maxi.first!=-1){
swap(slope[maxi.second],slope[node]);
for(auto i:g[node]){
if(i.first==maxi.second) continue;
for(ll j:slope[i.first]){
slope[node].emplace(j);
}
}
}
for(int i=0;i<ll(g[node].size())-1;i++){
auto it=slope[node].end();
it--;
slope[node].erase(it);
}
if(slope[node].empty()){
slope[node].emplace(c);
slope[node].emplace(c);
return;
}
if(c!=-1){
auto it=--slope[node].end();
ll posl=*it+c;
slope[node].erase(it);
it=--slope[node].end();
ll predposl=*it+c;
slope[node].erase(it);
slope[node].emplace(posl);
slope[node].emplace(predposl);
}
}
int main(){
ll n,m;
cin>>n>>m;
n+=m;
ll vr=0;
for(ll i=2;i<=n;i++){
ll p,c; cin>>p>>c;
g[p].push_back({i,c});
vr+=c;
}
dfs(1);
ll sl=0;
vector<ll> nes;
for(ll i:slope[1]) nes.push_back(i);
reverse(nes.begin(),nes.end());
nes.push_back(0);
for(ll i=1;i<ll(nes.size());i++){
vr-=sl*(nes[i-1]-nes[i]);
sl++;
}
cout<<vr;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21440 KB |
Output is correct |
2 |
Correct |
11 ms |
21332 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
11 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21332 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
13 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21332 KB |
Output is correct |
2 |
Correct |
11 ms |
21332 KB |
Output is correct |
3 |
Correct |
12 ms |
21332 KB |
Output is correct |
4 |
Correct |
11 ms |
21440 KB |
Output is correct |
5 |
Correct |
12 ms |
21444 KB |
Output is correct |
6 |
Correct |
11 ms |
21332 KB |
Output is correct |
7 |
Correct |
12 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21436 KB |
Output is correct |
9 |
Correct |
13 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21460 KB |
Output is correct |
11 |
Correct |
11 ms |
21460 KB |
Output is correct |
12 |
Correct |
11 ms |
21460 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21432 KB |
Output is correct |
16 |
Correct |
14 ms |
21460 KB |
Output is correct |
17 |
Correct |
12 ms |
21612 KB |
Output is correct |
18 |
Correct |
11 ms |
21460 KB |
Output is correct |
19 |
Correct |
11 ms |
21460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21440 KB |
Output is correct |
2 |
Correct |
11 ms |
21332 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
11 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21332 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
13 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21460 KB |
Output is correct |
11 |
Correct |
11 ms |
21332 KB |
Output is correct |
12 |
Correct |
11 ms |
21332 KB |
Output is correct |
13 |
Correct |
12 ms |
21332 KB |
Output is correct |
14 |
Correct |
11 ms |
21440 KB |
Output is correct |
15 |
Correct |
12 ms |
21444 KB |
Output is correct |
16 |
Correct |
11 ms |
21332 KB |
Output is correct |
17 |
Correct |
12 ms |
21460 KB |
Output is correct |
18 |
Correct |
11 ms |
21436 KB |
Output is correct |
19 |
Correct |
13 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
11 ms |
21460 KB |
Output is correct |
22 |
Correct |
11 ms |
21460 KB |
Output is correct |
23 |
Correct |
11 ms |
21460 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
11 ms |
21432 KB |
Output is correct |
26 |
Correct |
14 ms |
21460 KB |
Output is correct |
27 |
Correct |
12 ms |
21612 KB |
Output is correct |
28 |
Correct |
11 ms |
21460 KB |
Output is correct |
29 |
Correct |
11 ms |
21460 KB |
Output is correct |
30 |
Correct |
13 ms |
21460 KB |
Output is correct |
31 |
Correct |
12 ms |
21460 KB |
Output is correct |
32 |
Correct |
12 ms |
21588 KB |
Output is correct |
33 |
Correct |
12 ms |
21716 KB |
Output is correct |
34 |
Correct |
13 ms |
21716 KB |
Output is correct |
35 |
Correct |
17 ms |
21844 KB |
Output is correct |
36 |
Correct |
14 ms |
21972 KB |
Output is correct |
37 |
Correct |
17 ms |
21964 KB |
Output is correct |
38 |
Correct |
15 ms |
22100 KB |
Output is correct |
39 |
Correct |
15 ms |
22120 KB |
Output is correct |
40 |
Correct |
14 ms |
22232 KB |
Output is correct |
41 |
Correct |
14 ms |
22228 KB |
Output is correct |
42 |
Correct |
13 ms |
21588 KB |
Output is correct |
43 |
Correct |
14 ms |
22312 KB |
Output is correct |
44 |
Correct |
15 ms |
22228 KB |
Output is correct |
45 |
Correct |
15 ms |
22228 KB |
Output is correct |
46 |
Correct |
15 ms |
22612 KB |
Output is correct |
47 |
Correct |
15 ms |
22612 KB |
Output is correct |
48 |
Correct |
15 ms |
22356 KB |
Output is correct |
49 |
Correct |
15 ms |
22484 KB |
Output is correct |
50 |
Correct |
14 ms |
22356 KB |
Output is correct |
51 |
Correct |
14 ms |
22228 KB |
Output is correct |
52 |
Correct |
15 ms |
22356 KB |
Output is correct |
53 |
Correct |
15 ms |
22484 KB |
Output is correct |
54 |
Correct |
15 ms |
22612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21440 KB |
Output is correct |
2 |
Correct |
11 ms |
21332 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
11 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21332 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
13 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21460 KB |
Output is correct |
11 |
Correct |
11 ms |
21332 KB |
Output is correct |
12 |
Correct |
11 ms |
21332 KB |
Output is correct |
13 |
Correct |
12 ms |
21332 KB |
Output is correct |
14 |
Correct |
11 ms |
21440 KB |
Output is correct |
15 |
Correct |
12 ms |
21444 KB |
Output is correct |
16 |
Correct |
11 ms |
21332 KB |
Output is correct |
17 |
Correct |
12 ms |
21460 KB |
Output is correct |
18 |
Correct |
11 ms |
21436 KB |
Output is correct |
19 |
Correct |
13 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
11 ms |
21460 KB |
Output is correct |
22 |
Correct |
11 ms |
21460 KB |
Output is correct |
23 |
Correct |
11 ms |
21460 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
11 ms |
21432 KB |
Output is correct |
26 |
Correct |
14 ms |
21460 KB |
Output is correct |
27 |
Correct |
12 ms |
21612 KB |
Output is correct |
28 |
Correct |
11 ms |
21460 KB |
Output is correct |
29 |
Correct |
11 ms |
21460 KB |
Output is correct |
30 |
Correct |
13 ms |
21460 KB |
Output is correct |
31 |
Correct |
12 ms |
21460 KB |
Output is correct |
32 |
Correct |
12 ms |
21588 KB |
Output is correct |
33 |
Correct |
12 ms |
21716 KB |
Output is correct |
34 |
Correct |
13 ms |
21716 KB |
Output is correct |
35 |
Correct |
17 ms |
21844 KB |
Output is correct |
36 |
Correct |
14 ms |
21972 KB |
Output is correct |
37 |
Correct |
17 ms |
21964 KB |
Output is correct |
38 |
Correct |
15 ms |
22100 KB |
Output is correct |
39 |
Correct |
15 ms |
22120 KB |
Output is correct |
40 |
Correct |
14 ms |
22232 KB |
Output is correct |
41 |
Correct |
14 ms |
22228 KB |
Output is correct |
42 |
Correct |
13 ms |
21588 KB |
Output is correct |
43 |
Correct |
14 ms |
22312 KB |
Output is correct |
44 |
Correct |
15 ms |
22228 KB |
Output is correct |
45 |
Correct |
15 ms |
22228 KB |
Output is correct |
46 |
Correct |
15 ms |
22612 KB |
Output is correct |
47 |
Correct |
15 ms |
22612 KB |
Output is correct |
48 |
Correct |
15 ms |
22356 KB |
Output is correct |
49 |
Correct |
15 ms |
22484 KB |
Output is correct |
50 |
Correct |
14 ms |
22356 KB |
Output is correct |
51 |
Correct |
14 ms |
22228 KB |
Output is correct |
52 |
Correct |
15 ms |
22356 KB |
Output is correct |
53 |
Correct |
15 ms |
22484 KB |
Output is correct |
54 |
Correct |
15 ms |
22612 KB |
Output is correct |
55 |
Correct |
23 ms |
23252 KB |
Output is correct |
56 |
Correct |
54 ms |
28488 KB |
Output is correct |
57 |
Correct |
90 ms |
33724 KB |
Output is correct |
58 |
Correct |
117 ms |
37448 KB |
Output is correct |
59 |
Correct |
160 ms |
42588 KB |
Output is correct |
60 |
Correct |
203 ms |
47948 KB |
Output is correct |
61 |
Correct |
233 ms |
51912 KB |
Output is correct |
62 |
Correct |
261 ms |
55540 KB |
Output is correct |
63 |
Correct |
324 ms |
62740 KB |
Output is correct |
64 |
Correct |
340 ms |
63428 KB |
Output is correct |
65 |
Correct |
211 ms |
73268 KB |
Output is correct |
66 |
Correct |
211 ms |
73160 KB |
Output is correct |
67 |
Correct |
203 ms |
35916 KB |
Output is correct |
68 |
Correct |
287 ms |
75284 KB |
Output is correct |
69 |
Correct |
337 ms |
73288 KB |
Output is correct |
70 |
Correct |
338 ms |
73296 KB |
Output is correct |
71 |
Correct |
423 ms |
105052 KB |
Output is correct |
72 |
Correct |
372 ms |
104988 KB |
Output is correct |
73 |
Correct |
357 ms |
91764 KB |
Output is correct |
74 |
Correct |
390 ms |
91924 KB |
Output is correct |
75 |
Correct |
349 ms |
90692 KB |
Output is correct |
76 |
Correct |
347 ms |
90800 KB |
Output is correct |
77 |
Correct |
383 ms |
88608 KB |
Output is correct |
78 |
Correct |
367 ms |
85628 KB |
Output is correct |
79 |
Correct |
404 ms |
92328 KB |
Output is correct |