#include <bits/stdc++.h>
#pragma GCC optimize("03")
#pragma GCC target("avx2")
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define F first
#define S second
#define ld long double
#define pb push_back
#define all(v) v.begin(),v.end()
#define in insert
#define sz(s) (int)s.size()
#define int ll
#define ppb pop_back
#define mem(a,i) memset(a,i,sizeof(a))
using namespace std;
const int MAX=2e5+100;
const ll inf=2e18;
const int mod=1e9+7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,m;
vector<int> g[MAX];
int a[MAX];
multiset<int> st[MAX];
int slope[MAX];
void mrg(int v,int to){
slope[v]+=slope[to];
if(sz(st[v])<sz(st[to]))swap(st[v],st[to]);
for(int x:st[to])st[v].in(x);
st[to].clear();
}
void dfs(int v){
if(g[v].empty()){
slope[v]=-1;
st[v].in(a[v]);
st[v].in(a[v]);
return;
}
for(auto to:g[v]){
dfs(to);
mrg(v,to);
}
while(sz(st[v])+slope[v]>1){
st[v].erase(--st[v].end());
}
int A=*(--st[v].end());
st[v].erase(--st[v].end());
int B=*(--st[v].end());
st[v].erase(--st[v].end());
A+=a[v],B+=a[v];
st[v].in(A);st[v].in(B);
}
void solve(){
cin>>n>>m;
int ans=0;
for(int i=2;i<=n+m;i++){
int p;
cin>>p>>a[i];
g[p].pb(i);
ans+=a[i];
}
dfs(1);
int prev=0;
// cout<<ans<<"\n";
slope[1]++;
for(int i:st[1]){
// cout<<i<<" ";
ans+=slope[1]*i;
// cout<<i-prev<<" "<<slope[1]<<"\n";
slope[1]++;
}
cout<<ans;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--)solve();
}
Compilation message
fireworks.cpp: In function 'void solve()':
fireworks.cpp:74:6: warning: unused variable 'prev' [-Wunused-variable]
74 | int prev=0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
14428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |