#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(int 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(g[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<int> vec(1,0);
for(int 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 |
21336 KB |
Output is correct |
2 |
Correct |
4 ms |
21564 KB |
Output is correct |
3 |
Incorrect |
4 ms |
21596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21336 KB |
Output is correct |
2 |
Correct |
5 ms |
21584 KB |
Output is correct |
3 |
Correct |
4 ms |
21340 KB |
Output is correct |
4 |
Correct |
4 ms |
21340 KB |
Output is correct |
5 |
Correct |
4 ms |
21592 KB |
Output is correct |
6 |
Correct |
4 ms |
21596 KB |
Output is correct |
7 |
Correct |
5 ms |
21560 KB |
Output is correct |
8 |
Correct |
4 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 |
5 ms |
21572 KB |
Output is correct |
12 |
Correct |
4 ms |
21596 KB |
Output is correct |
13 |
Correct |
4 ms |
21592 KB |
Output is correct |
14 |
Correct |
5 ms |
21576 KB |
Output is correct |
15 |
Correct |
5 ms |
21556 KB |
Output is correct |
16 |
Correct |
5 ms |
21596 KB |
Output is correct |
17 |
Correct |
4 ms |
21596 KB |
Output is correct |
18 |
Correct |
4 ms |
21596 KB |
Output is correct |
19 |
Correct |
4 ms |
21592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21336 KB |
Output is correct |
2 |
Correct |
4 ms |
21564 KB |
Output is correct |
3 |
Incorrect |
4 ms |
21596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21336 KB |
Output is correct |
2 |
Correct |
4 ms |
21564 KB |
Output is correct |
3 |
Incorrect |
4 ms |
21596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |