#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define pb push_back
#define vec vector
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define fast_iati ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N=5e5+1;
const ll inf=1e18;
vec<pii> g[N];
ll h[N];
pll vl[N];
ll k[N],b[N];
ll ans=0;
void dfs(int v){
if(!sz(g[v])){
vl[v]={h[v],h[v]};
k[v]=1,b[v]=-h[v];
// cout<<"D "<<v<<' '<<h[v]<<endl;
}
else{
vec<int> st;
ll l=-inf,r=inf;
for(auto &z : g[v]){
h[z.f]=h[v]+z.s;
dfs(z.f);
umax(l,vl[z.f].f-z.s);
// umin(r,vl[z.f].s+z.s);
k[v]+=k[z.f];b[v]+=b[z.f];
st.pb(vl[z.f].f);st.pb(vl[z.f].s);
}
ll mn=inf;
sort(rall(st));
vec<ll>go;
ll _k=k[v],_b=b[v];
for(auto &z : st){
_k--;_b+=z;
umin(mn,_k*z+_b);
if(_k*z+_b==mn) go.pb(z);
}
reverse(all(go));
vl[v]={max(l,go[0]),min(r,go.back())};
ans+=mn;b[v]=-vl[v].s;k[v]=1;
// cout<<"DEBUG "<<v<<' '<<mn<<' '<<vl[v].f<<' '<<vl[v].s<<' '<<b[v]<<endl;
}
}
signed main(){
fast_iati;
int n,m;
cin>>n>>m;
for(int i=1;i<n+m;i++){
int v,x;
cin>>v>>x;--v;
g[v].pb({i,x});
}
for(int i=0;i<n;i++) assert(sz(g[i]));
for(int i=n;i<n+m;i++) assert(!sz(g[i]));
dfs(0);
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
12080 KB |
Output is correct |
3 |
Correct |
5 ms |
12032 KB |
Output is correct |
4 |
Correct |
6 ms |
12064 KB |
Output is correct |
5 |
Correct |
6 ms |
11984 KB |
Output is correct |
6 |
Correct |
6 ms |
12080 KB |
Output is correct |
7 |
Correct |
6 ms |
11984 KB |
Output is correct |
8 |
Correct |
6 ms |
11984 KB |
Output is correct |
9 |
Correct |
6 ms |
12028 KB |
Output is correct |
10 |
Correct |
6 ms |
12072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
11984 KB |
Output is correct |
3 |
Incorrect |
6 ms |
11984 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
12080 KB |
Output is correct |
3 |
Correct |
5 ms |
12032 KB |
Output is correct |
4 |
Correct |
6 ms |
12064 KB |
Output is correct |
5 |
Correct |
6 ms |
11984 KB |
Output is correct |
6 |
Correct |
6 ms |
12080 KB |
Output is correct |
7 |
Correct |
6 ms |
11984 KB |
Output is correct |
8 |
Correct |
6 ms |
11984 KB |
Output is correct |
9 |
Correct |
6 ms |
12028 KB |
Output is correct |
10 |
Correct |
6 ms |
12072 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11984 KB |
Output is correct |
13 |
Incorrect |
6 ms |
11984 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
12080 KB |
Output is correct |
3 |
Correct |
5 ms |
12032 KB |
Output is correct |
4 |
Correct |
6 ms |
12064 KB |
Output is correct |
5 |
Correct |
6 ms |
11984 KB |
Output is correct |
6 |
Correct |
6 ms |
12080 KB |
Output is correct |
7 |
Correct |
6 ms |
11984 KB |
Output is correct |
8 |
Correct |
6 ms |
11984 KB |
Output is correct |
9 |
Correct |
6 ms |
12028 KB |
Output is correct |
10 |
Correct |
6 ms |
12072 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
6 ms |
11984 KB |
Output is correct |
13 |
Incorrect |
6 ms |
11984 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |