#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[40*N],p[40*N];
int l[40*N],r[40*N];
ll d1,d2;
segtree(){
t[0]=0;
fill(t,t+40*N,0);
fill(l,l+40*N,0);
fill(r,r+40*N,0);
fill(p,p+40*N,0);
}
void push(int v,int tl,int tr){
if(!p[v])
return;
if(!l[v]) l[v]=tt++;
if(!r[v]) r[v]=tt++;
assert(tt<40*N);
for(auto &u : {l[v],r[v]})
p[u]+=p[v],t[u]+=p[v];
p[v]=0;
return;
}
void upd(int i,ll x,int &v,int tl=0,int tr=N-1){
if(!v)
v=tt++;
assert(tt<40*N);
if(tl==tr){
t[v]=x;
return;
}
int tm=(tl+tr)>>1;push(v,tl,tr);
if(tm>=i)
upd(i,x,l[v],tl,tm);
else
upd(i,x,r[v],tm+1,tr);
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;push(v,tl,tr);
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;
return u;
}
t[v]=d1+d2;
return v;
}
if(!v){
umin(d2,t[u]);
p[u]+=d1;
t[u]+=d1;
return u;
}
if(!u){
umin(d1,t[v]);
p[v]+=d2;
t[v]+=d2;
return v;
}
int tm=(tl+tr)>>1;
if(l[u] || r[u]) push(u,tl,tr);
if(l[v] || r[v]) push(v,tl,tr);
p[v]=p[u]=0;
r[v]=mg(r[v],r[u],tm+1,tr);
l[v]=mg(l[v],l[u],tl,tm);
t[v]=d1+d2;
return v;
}
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];
int rt[N],used[N],u[N];
vec<pil> cost[N];
vec<int> ord;
void dfs1(int v){
if(u[v])
return;
u[v]=1;
for(auto &z : g[v])
dfs1(z);
ord.pb(v);
}
void dfs(int v){
u[v]=0;
// cout<<"DFS "<<v<<endl;
for(auto &z : g[v]){
dfs(z);
root[v]=sega.Merge(root[v],root[z]);
}
for(auto &z : cost[v])
sega.upd(z.f,sega.get(z.f,N-1,root[v])-z.s,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];
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();
for(int i=0;i<n;i++){
if(used[i]) continue;
int v=i;
while(!used[v]){
// cout<<"CHECK "<<v<<endl;
used[v]=1;
v=a[v];
}
vec<pil> who;
map<int,ll> mp;
ll s=0;
int st=v;
while(used[v]==1){
mp[h[v]]+=c[v];
s+=c[v];
rt[v]=st;
used[v]=2;
// cerr<<"CYC "<<v<<' ';
v=a[v];
}
// cout<<endl;
for(auto &z : mp) cost[st].pb(z);
v=i;
while(used[v]==1){
rt[v]=v;
cost[v].pb({h[v],c[v]});
used[v]=2;
v=a[v];
}
}
for(int i=0;i<n;i++){
if(rt[i]!=rt[a[i]])
g[rt[a[i]]].pb(rt[i]);
}
for(int i=0;i<n;i++){
if(rt[i]==i)
dfs1(i);
}
// return 0;
reverse(all(ord));
for(auto &i : ord){
if(!u[i]) continue;
// cout<<"WT "<<i<<endl;
dfs(i);
sm+=sega.get(0,N-1,root[i]);
}
cout<<sm;
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 40 952129455
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
197540 KB |
Output is correct |
2 |
Correct |
84 ms |
197520 KB |
Output is correct |
3 |
Correct |
81 ms |
197624 KB |
Output is correct |
4 |
Correct |
94 ms |
197564 KB |
Output is correct |
5 |
Correct |
88 ms |
198112 KB |
Output is correct |
6 |
Correct |
94 ms |
198268 KB |
Output is correct |
7 |
Correct |
84 ms |
198132 KB |
Output is correct |
8 |
Correct |
87 ms |
198100 KB |
Output is correct |
9 |
Correct |
94 ms |
198048 KB |
Output is correct |
10 |
Correct |
91 ms |
198120 KB |
Output is correct |
11 |
Correct |
86 ms |
198064 KB |
Output is correct |
12 |
Correct |
84 ms |
198596 KB |
Output is correct |
13 |
Correct |
84 ms |
198608 KB |
Output is correct |
14 |
Correct |
102 ms |
198372 KB |
Output is correct |
15 |
Correct |
90 ms |
198380 KB |
Output is correct |
16 |
Correct |
88 ms |
198100 KB |
Output is correct |
17 |
Correct |
86 ms |
198136 KB |
Output is correct |
18 |
Correct |
81 ms |
198100 KB |
Output is correct |
19 |
Correct |
86 ms |
198368 KB |
Output is correct |
20 |
Correct |
92 ms |
198320 KB |
Output is correct |
21 |
Correct |
88 ms |
198308 KB |
Output is correct |
22 |
Correct |
94 ms |
198100 KB |
Output is correct |
23 |
Correct |
93 ms |
198080 KB |
Output is correct |
24 |
Correct |
85 ms |
198396 KB |
Output is correct |
25 |
Correct |
85 ms |
198296 KB |
Output is correct |
26 |
Correct |
84 ms |
198692 KB |
Output is correct |
27 |
Correct |
93 ms |
198288 KB |
Output is correct |
28 |
Correct |
94 ms |
198348 KB |
Output is correct |
29 |
Correct |
98 ms |
198468 KB |
Output is correct |
30 |
Correct |
86 ms |
198388 KB |
Output is correct |
31 |
Correct |
86 ms |
198360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
197540 KB |
Output is correct |
2 |
Correct |
84 ms |
197520 KB |
Output is correct |
3 |
Correct |
81 ms |
197624 KB |
Output is correct |
4 |
Correct |
94 ms |
197564 KB |
Output is correct |
5 |
Correct |
88 ms |
198112 KB |
Output is correct |
6 |
Correct |
94 ms |
198268 KB |
Output is correct |
7 |
Correct |
84 ms |
198132 KB |
Output is correct |
8 |
Correct |
87 ms |
198100 KB |
Output is correct |
9 |
Correct |
94 ms |
198048 KB |
Output is correct |
10 |
Correct |
91 ms |
198120 KB |
Output is correct |
11 |
Correct |
86 ms |
198064 KB |
Output is correct |
12 |
Correct |
84 ms |
198596 KB |
Output is correct |
13 |
Correct |
84 ms |
198608 KB |
Output is correct |
14 |
Correct |
102 ms |
198372 KB |
Output is correct |
15 |
Correct |
90 ms |
198380 KB |
Output is correct |
16 |
Correct |
88 ms |
198100 KB |
Output is correct |
17 |
Correct |
86 ms |
198136 KB |
Output is correct |
18 |
Correct |
81 ms |
198100 KB |
Output is correct |
19 |
Correct |
86 ms |
198368 KB |
Output is correct |
20 |
Correct |
92 ms |
198320 KB |
Output is correct |
21 |
Correct |
88 ms |
198308 KB |
Output is correct |
22 |
Correct |
94 ms |
198100 KB |
Output is correct |
23 |
Correct |
93 ms |
198080 KB |
Output is correct |
24 |
Correct |
85 ms |
198396 KB |
Output is correct |
25 |
Correct |
85 ms |
198296 KB |
Output is correct |
26 |
Correct |
84 ms |
198692 KB |
Output is correct |
27 |
Correct |
93 ms |
198288 KB |
Output is correct |
28 |
Correct |
94 ms |
198348 KB |
Output is correct |
29 |
Correct |
98 ms |
198468 KB |
Output is correct |
30 |
Correct |
86 ms |
198388 KB |
Output is correct |
31 |
Correct |
86 ms |
198360 KB |
Output is correct |
32 |
Correct |
101 ms |
198132 KB |
Output is correct |
33 |
Correct |
697 ms |
219312 KB |
Output is correct |
34 |
Correct |
431 ms |
219292 KB |
Output is correct |
35 |
Correct |
717 ms |
219292 KB |
Output is correct |
36 |
Correct |
453 ms |
219280 KB |
Output is correct |
37 |
Correct |
358 ms |
219240 KB |
Output is correct |
38 |
Correct |
317 ms |
219176 KB |
Output is correct |
39 |
Correct |
379 ms |
241184 KB |
Output is correct |
40 |
Correct |
272 ms |
241208 KB |
Output is correct |
41 |
Correct |
266 ms |
241108 KB |
Output is correct |
42 |
Correct |
330 ms |
231736 KB |
Output is correct |
43 |
Correct |
276 ms |
231712 KB |
Output is correct |
44 |
Correct |
546 ms |
219040 KB |
Output is correct |
45 |
Correct |
347 ms |
219228 KB |
Output is correct |
46 |
Correct |
220 ms |
219112 KB |
Output is correct |
47 |
Correct |
421 ms |
228540 KB |
Output is correct |
48 |
Correct |
296 ms |
228416 KB |
Output is correct |
49 |
Correct |
252 ms |
228664 KB |
Output is correct |
50 |
Correct |
406 ms |
216048 KB |
Output is correct |
51 |
Correct |
291 ms |
216052 KB |
Output is correct |
52 |
Correct |
432 ms |
229832 KB |
Output is correct |
53 |
Correct |
290 ms |
229916 KB |
Output is correct |
54 |
Correct |
254 ms |
241320 KB |
Output is correct |
55 |
Correct |
415 ms |
228136 KB |
Output is correct |
56 |
Correct |
333 ms |
233928 KB |
Output is correct |
57 |
Correct |
322 ms |
236328 KB |
Output is correct |
58 |
Correct |
283 ms |
231080 KB |
Output is correct |
59 |
Correct |
326 ms |
231016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
197540 KB |
Output is correct |
2 |
Correct |
84 ms |
197520 KB |
Output is correct |
3 |
Correct |
81 ms |
197624 KB |
Output is correct |
4 |
Correct |
94 ms |
197564 KB |
Output is correct |
5 |
Correct |
88 ms |
198112 KB |
Output is correct |
6 |
Correct |
94 ms |
198268 KB |
Output is correct |
7 |
Correct |
84 ms |
198132 KB |
Output is correct |
8 |
Correct |
87 ms |
198100 KB |
Output is correct |
9 |
Correct |
94 ms |
198048 KB |
Output is correct |
10 |
Correct |
91 ms |
198120 KB |
Output is correct |
11 |
Correct |
86 ms |
198064 KB |
Output is correct |
12 |
Correct |
84 ms |
198596 KB |
Output is correct |
13 |
Correct |
84 ms |
198608 KB |
Output is correct |
14 |
Correct |
102 ms |
198372 KB |
Output is correct |
15 |
Correct |
90 ms |
198380 KB |
Output is correct |
16 |
Correct |
88 ms |
198100 KB |
Output is correct |
17 |
Correct |
86 ms |
198136 KB |
Output is correct |
18 |
Correct |
81 ms |
198100 KB |
Output is correct |
19 |
Correct |
86 ms |
198368 KB |
Output is correct |
20 |
Correct |
92 ms |
198320 KB |
Output is correct |
21 |
Correct |
88 ms |
198308 KB |
Output is correct |
22 |
Correct |
94 ms |
198100 KB |
Output is correct |
23 |
Correct |
93 ms |
198080 KB |
Output is correct |
24 |
Correct |
85 ms |
198396 KB |
Output is correct |
25 |
Correct |
85 ms |
198296 KB |
Output is correct |
26 |
Correct |
84 ms |
198692 KB |
Output is correct |
27 |
Correct |
93 ms |
198288 KB |
Output is correct |
28 |
Correct |
94 ms |
198348 KB |
Output is correct |
29 |
Correct |
98 ms |
198468 KB |
Output is correct |
30 |
Correct |
86 ms |
198388 KB |
Output is correct |
31 |
Correct |
86 ms |
198360 KB |
Output is correct |
32 |
Correct |
101 ms |
198132 KB |
Output is correct |
33 |
Correct |
697 ms |
219312 KB |
Output is correct |
34 |
Correct |
431 ms |
219292 KB |
Output is correct |
35 |
Correct |
717 ms |
219292 KB |
Output is correct |
36 |
Correct |
453 ms |
219280 KB |
Output is correct |
37 |
Correct |
358 ms |
219240 KB |
Output is correct |
38 |
Correct |
317 ms |
219176 KB |
Output is correct |
39 |
Correct |
379 ms |
241184 KB |
Output is correct |
40 |
Correct |
272 ms |
241208 KB |
Output is correct |
41 |
Correct |
266 ms |
241108 KB |
Output is correct |
42 |
Correct |
330 ms |
231736 KB |
Output is correct |
43 |
Correct |
276 ms |
231712 KB |
Output is correct |
44 |
Correct |
546 ms |
219040 KB |
Output is correct |
45 |
Correct |
347 ms |
219228 KB |
Output is correct |
46 |
Correct |
220 ms |
219112 KB |
Output is correct |
47 |
Correct |
421 ms |
228540 KB |
Output is correct |
48 |
Correct |
296 ms |
228416 KB |
Output is correct |
49 |
Correct |
252 ms |
228664 KB |
Output is correct |
50 |
Correct |
406 ms |
216048 KB |
Output is correct |
51 |
Correct |
291 ms |
216052 KB |
Output is correct |
52 |
Correct |
432 ms |
229832 KB |
Output is correct |
53 |
Correct |
290 ms |
229916 KB |
Output is correct |
54 |
Correct |
254 ms |
241320 KB |
Output is correct |
55 |
Correct |
415 ms |
228136 KB |
Output is correct |
56 |
Correct |
333 ms |
233928 KB |
Output is correct |
57 |
Correct |
322 ms |
236328 KB |
Output is correct |
58 |
Correct |
283 ms |
231080 KB |
Output is correct |
59 |
Correct |
326 ms |
231016 KB |
Output is correct |
60 |
Correct |
79 ms |
197500 KB |
Output is correct |
61 |
Correct |
82 ms |
197528 KB |
Output is correct |
62 |
Correct |
85 ms |
197492 KB |
Output is correct |
63 |
Correct |
85 ms |
197480 KB |
Output is correct |
64 |
Correct |
654 ms |
220200 KB |
Output is correct |
65 |
Correct |
491 ms |
220176 KB |
Output is correct |
66 |
Correct |
714 ms |
220280 KB |
Output is correct |
67 |
Correct |
515 ms |
220264 KB |
Output is correct |
68 |
Correct |
389 ms |
220236 KB |
Output is correct |
69 |
Correct |
340 ms |
220072 KB |
Output is correct |
70 |
Correct |
393 ms |
220964 KB |
Output is correct |
71 |
Correct |
214 ms |
207560 KB |
Output is correct |
72 |
Correct |
435 ms |
224260 KB |
Output is correct |
73 |
Correct |
190 ms |
207492 KB |
Output is correct |
74 |
Correct |
395 ms |
216976 KB |
Output is correct |
75 |
Correct |
267 ms |
213316 KB |
Output is correct |
76 |
Correct |
211 ms |
213396 KB |
Output is correct |
77 |
Correct |
483 ms |
216916 KB |
Output is correct |
78 |
Correct |
270 ms |
213372 KB |
Output is correct |
79 |
Correct |
498 ms |
216880 KB |
Output is correct |
80 |
Correct |
366 ms |
214596 KB |
Output is correct |
81 |
Correct |
242 ms |
214624 KB |
Output is correct |
82 |
Correct |
330 ms |
224396 KB |
Output is correct |
83 |
Correct |
251 ms |
216072 KB |
Output is correct |
84 |
Correct |
434 ms |
221068 KB |
Output is correct |
85 |
Correct |
424 ms |
221176 KB |
Output is correct |
86 |
Correct |
391 ms |
219464 KB |
Output is correct |
87 |
Correct |
500 ms |
221352 KB |
Output is correct |
88 |
Correct |
414 ms |
222104 KB |
Output is correct |