#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
long long mainres,n,p[maxn],c[maxn],todor[maxn],vas[maxn];
set<long long>adj[maxn];
set<pair<long long,long long>>dp[maxn];
map<long long,long long>mp;
map<long long,long long>h[maxn];
void vorod(){
cin>>n;
for(long long i=1;i<=n;i++){
int d;
cin>>p[i]>>d>>c[i];
h[i][d]+=c[i];
mainres+=c[i];
if(i!=p[i]){
adj[p[i]].insert(i);
}
}
}
void merge(long long u,long long v){
long long fu=u;
if((long long)dp[u].size()<(long long)dp[v].size()){
swap(u,v);
}
for(auto x:dp[v]){
auto y=*dp[u].lower_bound(make_pair(x.first,-1));
if(y.first==x.first){
dp[u].erase(y);
y.second+=x.second;
dp[u].insert(y);
}else{
dp[u].insert(x);
}
}
if(fu!=u){
swap(dp[u],dp[fu]);
}
}
void ins(long long u,long long w,long long ww){
long long moonde=ww;
while(moonde>0&&(long long)dp[u].size()>0&&(*dp[u].begin()).first<w){
auto xx=dp[u].lower_bound(make_pair(w,-1));
xx--;
auto x=*xx;
if(x.second>=moonde){
dp[u].erase(x);
x.second-=moonde;
dp[u].insert(x);
break;
}else{
dp[u].erase(x);
moonde-=x.second;
}
}
if((long long)dp[u].size()==0||(*dp[u].begin()).first>w){
dp[u].insert(make_pair(w,ww));
return ;
}
auto xx=dp[u].lower_bound(make_pair(w,-1));
auto x=*xx;
if(x.first==w){
dp[u].erase(x);
x.second+=ww;
dp[u].insert(x);
}else{
dp[u].insert(make_pair(w,ww));
}
}
void solved(long long u){
//cout<<"wtf: "<<u<<endl;
for(auto x:adj[u]){
solved(x);
merge(u,x);
}
for(auto x:h[u]){
ins(u,x.first,x.second);
}
}
deque<int>wtf;
void cald(int u){
if(vas[u]){
while((int)wtf.size()>0&&wtf.back()!=u){
wtf.pop_back();
}
return ;
}
vas[u]=1;
wtf.push_front(u);
cald(p[u]);
}
void pre(){
for(int i=1;i<=n;i++){
if(p[i]==i||vas[i]){
continue;
}
cald(i);
if((int)wtf.size()<=1){
wtf.clear();
continue;
}
set<int>st;
for(auto x:wtf){
st.insert(x);
}
int u=wtf.front();
wtf.pop_front();
p[u]=u;
for(auto x:wtf){
for(auto y:h[x]){
h[u][y.first]+=y.second;
}
for(auto y:adj[x]){
if(st.count(y)==0){
adj[u].insert(y);
}
}
adj[u].erase(x);
}
wtf.clear();
}
}
void solve(){
for(int i=1;i<=n;i++){
if(p[i]!=i){
continue;
}
solved(i);
for(auto x:dp[i]){
mainres-=x.second;
}
}
}
void khor(){
cout<<mainres<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
//cout.tie(0);
//freopen("inp.txt","r",stdin);
vorod();
pre();
// //cout<<"ha"<<endl;
solve();
khor();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
31320 KB |
Output is correct |
2 |
Correct |
10 ms |
31324 KB |
Output is correct |
3 |
Correct |
8 ms |
31400 KB |
Output is correct |
4 |
Correct |
7 ms |
31320 KB |
Output is correct |
5 |
Correct |
13 ms |
32712 KB |
Output is correct |
6 |
Correct |
11 ms |
32344 KB |
Output is correct |
7 |
Correct |
10 ms |
32268 KB |
Output is correct |
8 |
Correct |
15 ms |
32860 KB |
Output is correct |
9 |
Correct |
11 ms |
32348 KB |
Output is correct |
10 |
Correct |
10 ms |
32092 KB |
Output is correct |
11 |
Correct |
9 ms |
32008 KB |
Output is correct |
12 |
Correct |
10 ms |
32092 KB |
Output is correct |
13 |
Correct |
9 ms |
32092 KB |
Output is correct |
14 |
Correct |
10 ms |
32076 KB |
Output is correct |
15 |
Correct |
10 ms |
32080 KB |
Output is correct |
16 |
Correct |
12 ms |
33116 KB |
Output is correct |
17 |
Correct |
13 ms |
32604 KB |
Output is correct |
18 |
Correct |
9 ms |
32092 KB |
Output is correct |
19 |
Correct |
11 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32172 KB |
Output is correct |
21 |
Correct |
15 ms |
32092 KB |
Output is correct |
22 |
Correct |
10 ms |
32344 KB |
Output is correct |
23 |
Correct |
11 ms |
32092 KB |
Output is correct |
24 |
Correct |
11 ms |
32172 KB |
Output is correct |
25 |
Correct |
10 ms |
32092 KB |
Output is correct |
26 |
Correct |
10 ms |
32460 KB |
Output is correct |
27 |
Correct |
10 ms |
32348 KB |
Output is correct |
28 |
Correct |
10 ms |
32208 KB |
Output is correct |
29 |
Correct |
9 ms |
32344 KB |
Output is correct |
30 |
Correct |
10 ms |
32348 KB |
Output is correct |
31 |
Correct |
14 ms |
32348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
31320 KB |
Output is correct |
2 |
Correct |
10 ms |
31324 KB |
Output is correct |
3 |
Correct |
8 ms |
31400 KB |
Output is correct |
4 |
Correct |
7 ms |
31320 KB |
Output is correct |
5 |
Correct |
13 ms |
32712 KB |
Output is correct |
6 |
Correct |
11 ms |
32344 KB |
Output is correct |
7 |
Correct |
10 ms |
32268 KB |
Output is correct |
8 |
Correct |
15 ms |
32860 KB |
Output is correct |
9 |
Correct |
11 ms |
32348 KB |
Output is correct |
10 |
Correct |
10 ms |
32092 KB |
Output is correct |
11 |
Correct |
9 ms |
32008 KB |
Output is correct |
12 |
Correct |
10 ms |
32092 KB |
Output is correct |
13 |
Correct |
9 ms |
32092 KB |
Output is correct |
14 |
Correct |
10 ms |
32076 KB |
Output is correct |
15 |
Correct |
10 ms |
32080 KB |
Output is correct |
16 |
Correct |
12 ms |
33116 KB |
Output is correct |
17 |
Correct |
13 ms |
32604 KB |
Output is correct |
18 |
Correct |
9 ms |
32092 KB |
Output is correct |
19 |
Correct |
11 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32172 KB |
Output is correct |
21 |
Correct |
15 ms |
32092 KB |
Output is correct |
22 |
Correct |
10 ms |
32344 KB |
Output is correct |
23 |
Correct |
11 ms |
32092 KB |
Output is correct |
24 |
Correct |
11 ms |
32172 KB |
Output is correct |
25 |
Correct |
10 ms |
32092 KB |
Output is correct |
26 |
Correct |
10 ms |
32460 KB |
Output is correct |
27 |
Correct |
10 ms |
32348 KB |
Output is correct |
28 |
Correct |
10 ms |
32208 KB |
Output is correct |
29 |
Correct |
9 ms |
32344 KB |
Output is correct |
30 |
Correct |
10 ms |
32348 KB |
Output is correct |
31 |
Correct |
14 ms |
32348 KB |
Output is correct |
32 |
Correct |
13 ms |
32860 KB |
Output is correct |
33 |
Correct |
391 ms |
109876 KB |
Output is correct |
34 |
Correct |
329 ms |
85076 KB |
Output is correct |
35 |
Correct |
359 ms |
106836 KB |
Output is correct |
36 |
Correct |
327 ms |
85152 KB |
Output is correct |
37 |
Correct |
220 ms |
66112 KB |
Output is correct |
38 |
Correct |
197 ms |
61776 KB |
Output is correct |
39 |
Correct |
225 ms |
65464 KB |
Output is correct |
40 |
Correct |
191 ms |
65020 KB |
Output is correct |
41 |
Correct |
114 ms |
64848 KB |
Output is correct |
42 |
Correct |
191 ms |
60240 KB |
Output is correct |
43 |
Correct |
190 ms |
60308 KB |
Output is correct |
44 |
Correct |
357 ms |
135252 KB |
Output is correct |
45 |
Correct |
272 ms |
99408 KB |
Output is correct |
46 |
Correct |
100 ms |
61664 KB |
Output is correct |
47 |
Correct |
300 ms |
73592 KB |
Output is correct |
48 |
Correct |
177 ms |
66364 KB |
Output is correct |
49 |
Correct |
110 ms |
66388 KB |
Output is correct |
50 |
Correct |
280 ms |
80644 KB |
Output is correct |
51 |
Correct |
176 ms |
68180 KB |
Output is correct |
52 |
Correct |
282 ms |
73488 KB |
Output is correct |
53 |
Correct |
186 ms |
66548 KB |
Output is correct |
54 |
Correct |
217 ms |
77292 KB |
Output is correct |
55 |
Correct |
271 ms |
75660 KB |
Output is correct |
56 |
Correct |
181 ms |
75600 KB |
Output is correct |
57 |
Correct |
173 ms |
76084 KB |
Output is correct |
58 |
Correct |
213 ms |
78836 KB |
Output is correct |
59 |
Correct |
233 ms |
78808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
31320 KB |
Output is correct |
2 |
Correct |
10 ms |
31324 KB |
Output is correct |
3 |
Correct |
8 ms |
31400 KB |
Output is correct |
4 |
Correct |
7 ms |
31320 KB |
Output is correct |
5 |
Correct |
13 ms |
32712 KB |
Output is correct |
6 |
Correct |
11 ms |
32344 KB |
Output is correct |
7 |
Correct |
10 ms |
32268 KB |
Output is correct |
8 |
Correct |
15 ms |
32860 KB |
Output is correct |
9 |
Correct |
11 ms |
32348 KB |
Output is correct |
10 |
Correct |
10 ms |
32092 KB |
Output is correct |
11 |
Correct |
9 ms |
32008 KB |
Output is correct |
12 |
Correct |
10 ms |
32092 KB |
Output is correct |
13 |
Correct |
9 ms |
32092 KB |
Output is correct |
14 |
Correct |
10 ms |
32076 KB |
Output is correct |
15 |
Correct |
10 ms |
32080 KB |
Output is correct |
16 |
Correct |
12 ms |
33116 KB |
Output is correct |
17 |
Correct |
13 ms |
32604 KB |
Output is correct |
18 |
Correct |
9 ms |
32092 KB |
Output is correct |
19 |
Correct |
11 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32172 KB |
Output is correct |
21 |
Correct |
15 ms |
32092 KB |
Output is correct |
22 |
Correct |
10 ms |
32344 KB |
Output is correct |
23 |
Correct |
11 ms |
32092 KB |
Output is correct |
24 |
Correct |
11 ms |
32172 KB |
Output is correct |
25 |
Correct |
10 ms |
32092 KB |
Output is correct |
26 |
Correct |
10 ms |
32460 KB |
Output is correct |
27 |
Correct |
10 ms |
32348 KB |
Output is correct |
28 |
Correct |
10 ms |
32208 KB |
Output is correct |
29 |
Correct |
9 ms |
32344 KB |
Output is correct |
30 |
Correct |
10 ms |
32348 KB |
Output is correct |
31 |
Correct |
14 ms |
32348 KB |
Output is correct |
32 |
Correct |
13 ms |
32860 KB |
Output is correct |
33 |
Correct |
391 ms |
109876 KB |
Output is correct |
34 |
Correct |
329 ms |
85076 KB |
Output is correct |
35 |
Correct |
359 ms |
106836 KB |
Output is correct |
36 |
Correct |
327 ms |
85152 KB |
Output is correct |
37 |
Correct |
220 ms |
66112 KB |
Output is correct |
38 |
Correct |
197 ms |
61776 KB |
Output is correct |
39 |
Correct |
225 ms |
65464 KB |
Output is correct |
40 |
Correct |
191 ms |
65020 KB |
Output is correct |
41 |
Correct |
114 ms |
64848 KB |
Output is correct |
42 |
Correct |
191 ms |
60240 KB |
Output is correct |
43 |
Correct |
190 ms |
60308 KB |
Output is correct |
44 |
Correct |
357 ms |
135252 KB |
Output is correct |
45 |
Correct |
272 ms |
99408 KB |
Output is correct |
46 |
Correct |
100 ms |
61664 KB |
Output is correct |
47 |
Correct |
300 ms |
73592 KB |
Output is correct |
48 |
Correct |
177 ms |
66364 KB |
Output is correct |
49 |
Correct |
110 ms |
66388 KB |
Output is correct |
50 |
Correct |
280 ms |
80644 KB |
Output is correct |
51 |
Correct |
176 ms |
68180 KB |
Output is correct |
52 |
Correct |
282 ms |
73488 KB |
Output is correct |
53 |
Correct |
186 ms |
66548 KB |
Output is correct |
54 |
Correct |
217 ms |
77292 KB |
Output is correct |
55 |
Correct |
271 ms |
75660 KB |
Output is correct |
56 |
Correct |
181 ms |
75600 KB |
Output is correct |
57 |
Correct |
173 ms |
76084 KB |
Output is correct |
58 |
Correct |
213 ms |
78836 KB |
Output is correct |
59 |
Correct |
233 ms |
78808 KB |
Output is correct |
60 |
Correct |
6 ms |
31324 KB |
Output is correct |
61 |
Correct |
6 ms |
31324 KB |
Output is correct |
62 |
Correct |
6 ms |
31324 KB |
Output is correct |
63 |
Correct |
6 ms |
31324 KB |
Output is correct |
64 |
Correct |
369 ms |
94176 KB |
Output is correct |
65 |
Correct |
301 ms |
77344 KB |
Output is correct |
66 |
Correct |
345 ms |
95096 KB |
Output is correct |
67 |
Correct |
347 ms |
77140 KB |
Output is correct |
68 |
Correct |
226 ms |
63404 KB |
Output is correct |
69 |
Correct |
232 ms |
65472 KB |
Output is correct |
70 |
Correct |
436 ms |
78932 KB |
Output is correct |
71 |
Correct |
270 ms |
66252 KB |
Output is correct |
72 |
Correct |
516 ms |
83268 KB |
Output is correct |
73 |
Correct |
293 ms |
70816 KB |
Output is correct |
74 |
Correct |
606 ms |
85848 KB |
Output is correct |
75 |
Correct |
393 ms |
71628 KB |
Output is correct |
76 |
Correct |
345 ms |
71600 KB |
Output is correct |
77 |
Correct |
438 ms |
85612 KB |
Output is correct |
78 |
Correct |
285 ms |
71756 KB |
Output is correct |
79 |
Correct |
433 ms |
95076 KB |
Output is correct |
80 |
Correct |
308 ms |
77012 KB |
Output is correct |
81 |
Correct |
257 ms |
66224 KB |
Output is correct |
82 |
Correct |
475 ms |
85908 KB |
Output is correct |
83 |
Correct |
77 ms |
62804 KB |
Output is correct |
84 |
Correct |
398 ms |
78236 KB |
Output is correct |
85 |
Correct |
369 ms |
78232 KB |
Output is correct |
86 |
Correct |
389 ms |
75148 KB |
Output is correct |
87 |
Correct |
386 ms |
78076 KB |
Output is correct |
88 |
Correct |
375 ms |
78220 KB |
Output is correct |