#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;
SPQ(ll) R;
PQ(ll) L;
SLP(ll L_,ll R_){
R.push(R_);L.push(L_);
}
void insertL(ll x){
c+=max(0ll,x-(R.top()+add_R));
R.push(x-add_R);L.push(R.top()+add_R-add_L);R.pop();
}
void insertR(ll x){
c+=max(0ll,L.top()+add_L-x);
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;
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,inf));
function<void(ll)> dfs=[&](ll i){
if(g[i].size()==0){
dp[i].R.push(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].add_L);dp[x.fi].L.pop();
}
while(dp[x.fi].R.size()){
dp[i].insertR(dp[x.fi].R.top()+dp[x.fi].add_R);dp[x.fi].R.pop();
}
}
/*
out(i);
out(dp[i].c);
auto l=dp[i].L;
auto r=dp[i].R;
vi vl,vr;
while(l.size()){
vl.pb(l.top());l.pop();
}
while(r.size()){
vr.pb(r.top());r.pop();
}
outv(vl);outv(vr);
cout<<endl;
*/
};dfs(0);
out(dp[0].c);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
616 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
5 ms |
852 KB |
Output is correct |
34 |
Correct |
4 ms |
980 KB |
Output is correct |
35 |
Correct |
7 ms |
1236 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
6 ms |
1492 KB |
Output is correct |
38 |
Correct |
7 ms |
1620 KB |
Output is correct |
39 |
Correct |
8 ms |
1572 KB |
Output is correct |
40 |
Correct |
7 ms |
2388 KB |
Output is correct |
41 |
Correct |
6 ms |
2404 KB |
Output is correct |
42 |
Correct |
6 ms |
1492 KB |
Output is correct |
43 |
Correct |
5 ms |
1876 KB |
Output is correct |
44 |
Correct |
6 ms |
1748 KB |
Output is correct |
45 |
Correct |
6 ms |
1724 KB |
Output is correct |
46 |
Correct |
6 ms |
1492 KB |
Output is correct |
47 |
Correct |
6 ms |
1492 KB |
Output is correct |
48 |
Correct |
5 ms |
1364 KB |
Output is correct |
49 |
Correct |
5 ms |
1492 KB |
Output is correct |
50 |
Correct |
5 ms |
1208 KB |
Output is correct |
51 |
Correct |
5 ms |
1236 KB |
Output is correct |
52 |
Correct |
5 ms |
1364 KB |
Output is correct |
53 |
Correct |
6 ms |
1364 KB |
Output is correct |
54 |
Correct |
5 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
616 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
5 ms |
852 KB |
Output is correct |
34 |
Correct |
4 ms |
980 KB |
Output is correct |
35 |
Correct |
7 ms |
1236 KB |
Output is correct |
36 |
Correct |
7 ms |
1236 KB |
Output is correct |
37 |
Correct |
6 ms |
1492 KB |
Output is correct |
38 |
Correct |
7 ms |
1620 KB |
Output is correct |
39 |
Correct |
8 ms |
1572 KB |
Output is correct |
40 |
Correct |
7 ms |
2388 KB |
Output is correct |
41 |
Correct |
6 ms |
2404 KB |
Output is correct |
42 |
Correct |
6 ms |
1492 KB |
Output is correct |
43 |
Correct |
5 ms |
1876 KB |
Output is correct |
44 |
Correct |
6 ms |
1748 KB |
Output is correct |
45 |
Correct |
6 ms |
1724 KB |
Output is correct |
46 |
Correct |
6 ms |
1492 KB |
Output is correct |
47 |
Correct |
6 ms |
1492 KB |
Output is correct |
48 |
Correct |
5 ms |
1364 KB |
Output is correct |
49 |
Correct |
5 ms |
1492 KB |
Output is correct |
50 |
Correct |
5 ms |
1208 KB |
Output is correct |
51 |
Correct |
5 ms |
1236 KB |
Output is correct |
52 |
Correct |
5 ms |
1364 KB |
Output is correct |
53 |
Correct |
6 ms |
1364 KB |
Output is correct |
54 |
Correct |
5 ms |
1364 KB |
Output is correct |
55 |
Correct |
19 ms |
3668 KB |
Output is correct |
56 |
Correct |
80 ms |
13040 KB |
Output is correct |
57 |
Correct |
145 ms |
22336 KB |
Output is correct |
58 |
Correct |
172 ms |
28480 KB |
Output is correct |
59 |
Correct |
251 ms |
38516 KB |
Output is correct |
60 |
Correct |
297 ms |
47324 KB |
Output is correct |
61 |
Correct |
353 ms |
53696 KB |
Output is correct |
62 |
Correct |
403 ms |
59536 KB |
Output is correct |
63 |
Correct |
462 ms |
73612 KB |
Output is correct |
64 |
Correct |
495 ms |
76868 KB |
Output is correct |
65 |
Correct |
339 ms |
126780 KB |
Output is correct |
66 |
Correct |
339 ms |
126828 KB |
Output is correct |
67 |
Correct |
379 ms |
73460 KB |
Output is correct |
68 |
Correct |
333 ms |
96168 KB |
Output is correct |
69 |
Correct |
398 ms |
92780 KB |
Output is correct |
70 |
Correct |
399 ms |
92752 KB |
Output is correct |
71 |
Correct |
353 ms |
75492 KB |
Output is correct |
72 |
Correct |
372 ms |
75432 KB |
Output is correct |
73 |
Correct |
330 ms |
71484 KB |
Output is correct |
74 |
Correct |
348 ms |
71880 KB |
Output is correct |
75 |
Correct |
345 ms |
70088 KB |
Output is correct |
76 |
Correct |
341 ms |
70076 KB |
Output is correct |
77 |
Correct |
361 ms |
68992 KB |
Output is correct |
78 |
Correct |
345 ms |
68232 KB |
Output is correct |
79 |
Correct |
280 ms |
70404 KB |
Output is correct |