#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define popcnt __builtin_popcountll
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
struct SLP{
ll c=0;
ll add_L=0,add_R=0;
PQ(ll) R;
SPQ(ll) L;
SLP(ll L_,ll R_){
R.push(R_);L.push(L_);
}
void addR(ll x){
add_R+=x;
}
void insertL(ll x){
c+=max(0ll,L.top()+add_L-x);
R.push(x-add_R);L.push(R.top()+add_R-add_L);R.pop();
}
void insertR(ll x){
c+=max(0ll,x-(R.top()+add_R));
L.push(x-add_L);R.push(L.top()+add_L-add_R);L.pop();
}
void upd(ll x){
ll t=L.top();L.pop();
L.push(t+x);
add_R+=x;
}
void updR(){
ll t=R.top();
while(R.size())R.pop();
R.push(t);
}
ll sz(){
return L.size()+R.size();
}
};
int main(){
ll n,m;cin>>n>>m;
n+=m;
vvp g(n);
REP(i,1,n){
ll a,b;cin>>a>>b;a--;
g[a].pb(i,b);
}
vector<SLP> dp(n,SLP(0,0));
function<void(ll)> dfs=[&](ll i){
if(g[i].size()==0)return;
for(auto x:g[i]){
dfs(x.fi);
dp[x.fi].upd(x.se);
if(dp[i].sz()<dp[x.fi].sz())swap(dp[i],dp[x.fi]);
dp[i].c+=dp[x.fi].c;
while(dp[x.fi].L.size()){
dp[i].insertL(dp[x.fi].L.top());dp[x.fi].L.pop();
}
while(dp[x.fi].R.size()){
dp[i].insertR(dp[x.fi].R.top());dp[x.fi].R.pop();
}
}
dp[i].updR();
};dfs(0);
out(dp[0].c);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |