#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<int,ll> pil;
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);}
const int N=2e5+1;
const ll inf=1e18;
vec<int> g[N];
int c[N],a[N],h[N];
int tt=1;
struct segtree{
ll t[20*N];
int l[20*N],r[20*N];
ll d1,d2;
segtree(){
t[0]=0;
fill(t,t+20*N,0);
fill(l,l+20*N,0);
fill(r,r+20*N,0);
}
void upd(int i,ll x,int &v,int tl=0,int tr=N-1){
if(!v)
v=tt++;
if(tl==tr){
// cout<<"UPD "<<v<<' '<<tl<<' '<<x<<endl;
t[v]=x;
return;
}
int tm=(tl+tr)>>1;
if(tm>=i)
upd(i,x,l[v],tl,tm);
else
upd(i,x,r[v],tm+1,tr);
// cout<<"YO "<<v<<' '<<l[v]<<' '<<r[v]<<endl;
t[v]=min(t[l[v]],t[r[v]]);
}
ll get(int l1,int r1,int v,int tl=0,int tr=N-1){
if(!v || tl>r1 || tr<l1)
return 0;
if(tl>=l1&&tr<=r1)
return t[v];
int tm=(tl+tr)>>1;
return min(get(l1,r1,l[v],tl,tm),get(l1,r1,r[v],tm+1,tr));
}
int mg(int v,int u,int tl,int tr){
if(!v && !u)
return 0;
if(tl==tr){
umin(d1,t[v]);umin(d2,t[u]);
if(!v){
t[u]=d1+d2;
// cerr<<"HEYT "<<d1+d2<<endl;
return u;
}
t[v]=d1+d2;
return v;
}
int tm=(tl+tr)>>1;
if(v){
r[v]=mg(r[v],r[u],tm+1,tr);
l[v]=mg(l[v],l[u],tl,tm);
t[v]=min(t[l[v]],t[r[v]]);
// cout<<"WT "<<v<<' '<<t[v]<<' '<<tl<<' '<<tr<<endl;
return v;
}
else{
r[u]=mg(r[v],r[u],tm+1,tr);
l[u]=mg(l[v],l[u],tl,tm);
t[u]=min(t[l[u]],t[r[u]]);
// cout<<"WT "<<u<<' '<<t[u]<<' '<<tl<<' '<<tr<<endl;
return u;
}
}
int Merge(int v,int u){
d1=d2=0;
//
return mg(v,u,0,N-1);
}
}sega;
ll toadd=0;
//map<int,ll> mp;
int root[N];
void dfs(int v){
// cout<<"DFS "<<v<<endl;
for(auto &z : g[v]){
dfs(z);
root[v]=sega.Merge(root[v],root[z]);
// cerr<<"EBAL "<<root[v]<<endl;
}
// cout<<"W "<<root[v]<<endl;
// cout<<sega.get(roo)
// cout<<"HEY "<<sega.get(root[v],h[v],N-1)<<endl;
sega.upd(h[v],sega.get(h[v],N-1,root[v])-c[v],root[v]);
}
signed main(){
fast_resp;
int n;
cin>>n;
vec<int>kek;
ll sm=0;
for(int i=0;i<n;i++){
cin>>a[i]>>h[i]>>c[i];--a[i];
if(a[i]!=i) g[a[i]].pb(i);
kek.pb(h[i]);sm+=c[i];
}
sort(all(kek));kek.erase(unique(all(kek)),kek.end());
for(int i=0;i<n;i++) h[i]=lower_bound(all(kek),h[i])-kek.begin();
dfs(0);
cout<<sm+sega.get(0,N-1,root[0]);
return 0;
}
/*
3
1 3 6
1 2 5
1 5 6
6
1 6 5
1 3 6
1 8 4
3 4 9
2 2 5
2 5 6
2
1 89 964898447
2 20 952129455
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
67608 KB |
Output is correct |
2 |
Correct |
28 ms |
67660 KB |
Output is correct |
3 |
Correct |
28 ms |
67524 KB |
Output is correct |
4 |
Correct |
29 ms |
67544 KB |
Output is correct |
5 |
Correct |
44 ms |
67912 KB |
Output is correct |
6 |
Correct |
38 ms |
67912 KB |
Output is correct |
7 |
Correct |
32 ms |
67868 KB |
Output is correct |
8 |
Correct |
44 ms |
67952 KB |
Output is correct |
9 |
Correct |
41 ms |
67864 KB |
Output is correct |
10 |
Correct |
37 ms |
67868 KB |
Output is correct |
11 |
Correct |
30 ms |
67928 KB |
Output is correct |
12 |
Correct |
331 ms |
68468 KB |
Output is correct |
13 |
Correct |
30 ms |
68424 KB |
Output is correct |
14 |
Correct |
265 ms |
68168 KB |
Output is correct |
15 |
Correct |
39 ms |
68200 KB |
Output is correct |
16 |
Correct |
39 ms |
67924 KB |
Output is correct |
17 |
Correct |
34 ms |
67924 KB |
Output is correct |
18 |
Correct |
37 ms |
67864 KB |
Output is correct |
19 |
Correct |
431 ms |
68320 KB |
Output is correct |
20 |
Correct |
41 ms |
68188 KB |
Output is correct |
21 |
Correct |
31 ms |
68108 KB |
Output is correct |
22 |
Correct |
432 ms |
68024 KB |
Output is correct |
23 |
Correct |
40 ms |
67916 KB |
Output is correct |
24 |
Correct |
455 ms |
68192 KB |
Output is correct |
25 |
Correct |
35 ms |
68172 KB |
Output is correct |
26 |
Correct |
184 ms |
68496 KB |
Output is correct |
27 |
Correct |
321 ms |
68116 KB |
Output is correct |
28 |
Correct |
257 ms |
68240 KB |
Output is correct |
29 |
Correct |
229 ms |
68364 KB |
Output is correct |
30 |
Correct |
130 ms |
68236 KB |
Output is correct |
31 |
Correct |
116 ms |
68132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
67608 KB |
Output is correct |
2 |
Correct |
28 ms |
67660 KB |
Output is correct |
3 |
Correct |
28 ms |
67524 KB |
Output is correct |
4 |
Correct |
29 ms |
67544 KB |
Output is correct |
5 |
Correct |
44 ms |
67912 KB |
Output is correct |
6 |
Correct |
38 ms |
67912 KB |
Output is correct |
7 |
Correct |
32 ms |
67868 KB |
Output is correct |
8 |
Correct |
44 ms |
67952 KB |
Output is correct |
9 |
Correct |
41 ms |
67864 KB |
Output is correct |
10 |
Correct |
37 ms |
67868 KB |
Output is correct |
11 |
Correct |
30 ms |
67928 KB |
Output is correct |
12 |
Correct |
331 ms |
68468 KB |
Output is correct |
13 |
Correct |
30 ms |
68424 KB |
Output is correct |
14 |
Correct |
265 ms |
68168 KB |
Output is correct |
15 |
Correct |
39 ms |
68200 KB |
Output is correct |
16 |
Correct |
39 ms |
67924 KB |
Output is correct |
17 |
Correct |
34 ms |
67924 KB |
Output is correct |
18 |
Correct |
37 ms |
67864 KB |
Output is correct |
19 |
Correct |
431 ms |
68320 KB |
Output is correct |
20 |
Correct |
41 ms |
68188 KB |
Output is correct |
21 |
Correct |
31 ms |
68108 KB |
Output is correct |
22 |
Correct |
432 ms |
68024 KB |
Output is correct |
23 |
Correct |
40 ms |
67916 KB |
Output is correct |
24 |
Correct |
455 ms |
68192 KB |
Output is correct |
25 |
Correct |
35 ms |
68172 KB |
Output is correct |
26 |
Correct |
184 ms |
68496 KB |
Output is correct |
27 |
Correct |
321 ms |
68116 KB |
Output is correct |
28 |
Correct |
257 ms |
68240 KB |
Output is correct |
29 |
Correct |
229 ms |
68364 KB |
Output is correct |
30 |
Correct |
130 ms |
68236 KB |
Output is correct |
31 |
Correct |
116 ms |
68132 KB |
Output is correct |
32 |
Correct |
46 ms |
67916 KB |
Output is correct |
33 |
Execution timed out |
2081 ms |
79892 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
67608 KB |
Output is correct |
2 |
Correct |
28 ms |
67660 KB |
Output is correct |
3 |
Correct |
28 ms |
67524 KB |
Output is correct |
4 |
Correct |
29 ms |
67544 KB |
Output is correct |
5 |
Correct |
44 ms |
67912 KB |
Output is correct |
6 |
Correct |
38 ms |
67912 KB |
Output is correct |
7 |
Correct |
32 ms |
67868 KB |
Output is correct |
8 |
Correct |
44 ms |
67952 KB |
Output is correct |
9 |
Correct |
41 ms |
67864 KB |
Output is correct |
10 |
Correct |
37 ms |
67868 KB |
Output is correct |
11 |
Correct |
30 ms |
67928 KB |
Output is correct |
12 |
Correct |
331 ms |
68468 KB |
Output is correct |
13 |
Correct |
30 ms |
68424 KB |
Output is correct |
14 |
Correct |
265 ms |
68168 KB |
Output is correct |
15 |
Correct |
39 ms |
68200 KB |
Output is correct |
16 |
Correct |
39 ms |
67924 KB |
Output is correct |
17 |
Correct |
34 ms |
67924 KB |
Output is correct |
18 |
Correct |
37 ms |
67864 KB |
Output is correct |
19 |
Correct |
431 ms |
68320 KB |
Output is correct |
20 |
Correct |
41 ms |
68188 KB |
Output is correct |
21 |
Correct |
31 ms |
68108 KB |
Output is correct |
22 |
Correct |
432 ms |
68024 KB |
Output is correct |
23 |
Correct |
40 ms |
67916 KB |
Output is correct |
24 |
Correct |
455 ms |
68192 KB |
Output is correct |
25 |
Correct |
35 ms |
68172 KB |
Output is correct |
26 |
Correct |
184 ms |
68496 KB |
Output is correct |
27 |
Correct |
321 ms |
68116 KB |
Output is correct |
28 |
Correct |
257 ms |
68240 KB |
Output is correct |
29 |
Correct |
229 ms |
68364 KB |
Output is correct |
30 |
Correct |
130 ms |
68236 KB |
Output is correct |
31 |
Correct |
116 ms |
68132 KB |
Output is correct |
32 |
Correct |
46 ms |
67916 KB |
Output is correct |
33 |
Execution timed out |
2081 ms |
79892 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |