#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<int,int>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);
}
vector<pair<int,int>>insy;
for(auto x:h[u]){
insy.push_back(x);
}
// reverse(insy.begin(),insy.end());
for(auto x:insy){
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();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
31320 KB |
Output is correct |
2 |
Correct |
6 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31408 KB |
Output is correct |
5 |
Correct |
13 ms |
32860 KB |
Output is correct |
6 |
Correct |
12 ms |
32460 KB |
Output is correct |
7 |
Correct |
10 ms |
32092 KB |
Output is correct |
8 |
Correct |
12 ms |
32860 KB |
Output is correct |
9 |
Correct |
11 ms |
32348 KB |
Output is correct |
10 |
Correct |
11 ms |
32236 KB |
Output is correct |
11 |
Correct |
9 ms |
32088 KB |
Output is correct |
12 |
Correct |
11 ms |
32456 KB |
Output is correct |
13 |
Correct |
9 ms |
32348 KB |
Output is correct |
14 |
Correct |
10 ms |
32092 KB |
Output is correct |
15 |
Correct |
10 ms |
32092 KB |
Output is correct |
16 |
Correct |
13 ms |
33116 KB |
Output is correct |
17 |
Correct |
11 ms |
32604 KB |
Output is correct |
18 |
Correct |
9 ms |
32356 KB |
Output is correct |
19 |
Correct |
12 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32348 KB |
Output is correct |
21 |
Correct |
9 ms |
32348 KB |
Output is correct |
22 |
Correct |
11 ms |
32604 KB |
Output is correct |
23 |
Correct |
10 ms |
32184 KB |
Output is correct |
24 |
Correct |
11 ms |
32352 KB |
Output is correct |
25 |
Correct |
9 ms |
32348 KB |
Output is correct |
26 |
Correct |
10 ms |
32792 KB |
Output is correct |
27 |
Correct |
11 ms |
32604 KB |
Output is correct |
28 |
Correct |
12 ms |
32604 KB |
Output is correct |
29 |
Correct |
12 ms |
32604 KB |
Output is correct |
30 |
Correct |
10 ms |
32600 KB |
Output is correct |
31 |
Correct |
11 ms |
32604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
31320 KB |
Output is correct |
2 |
Correct |
6 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31408 KB |
Output is correct |
5 |
Correct |
13 ms |
32860 KB |
Output is correct |
6 |
Correct |
12 ms |
32460 KB |
Output is correct |
7 |
Correct |
10 ms |
32092 KB |
Output is correct |
8 |
Correct |
12 ms |
32860 KB |
Output is correct |
9 |
Correct |
11 ms |
32348 KB |
Output is correct |
10 |
Correct |
11 ms |
32236 KB |
Output is correct |
11 |
Correct |
9 ms |
32088 KB |
Output is correct |
12 |
Correct |
11 ms |
32456 KB |
Output is correct |
13 |
Correct |
9 ms |
32348 KB |
Output is correct |
14 |
Correct |
10 ms |
32092 KB |
Output is correct |
15 |
Correct |
10 ms |
32092 KB |
Output is correct |
16 |
Correct |
13 ms |
33116 KB |
Output is correct |
17 |
Correct |
11 ms |
32604 KB |
Output is correct |
18 |
Correct |
9 ms |
32356 KB |
Output is correct |
19 |
Correct |
12 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32348 KB |
Output is correct |
21 |
Correct |
9 ms |
32348 KB |
Output is correct |
22 |
Correct |
11 ms |
32604 KB |
Output is correct |
23 |
Correct |
10 ms |
32184 KB |
Output is correct |
24 |
Correct |
11 ms |
32352 KB |
Output is correct |
25 |
Correct |
9 ms |
32348 KB |
Output is correct |
26 |
Correct |
10 ms |
32792 KB |
Output is correct |
27 |
Correct |
11 ms |
32604 KB |
Output is correct |
28 |
Correct |
12 ms |
32604 KB |
Output is correct |
29 |
Correct |
12 ms |
32604 KB |
Output is correct |
30 |
Correct |
10 ms |
32600 KB |
Output is correct |
31 |
Correct |
11 ms |
32604 KB |
Output is correct |
32 |
Correct |
12 ms |
32972 KB |
Output is correct |
33 |
Correct |
380 ms |
109720 KB |
Output is correct |
34 |
Correct |
320 ms |
84844 KB |
Output is correct |
35 |
Correct |
348 ms |
106696 KB |
Output is correct |
36 |
Correct |
297 ms |
84620 KB |
Output is correct |
37 |
Correct |
224 ms |
65848 KB |
Output is correct |
38 |
Correct |
211 ms |
61268 KB |
Output is correct |
39 |
Correct |
198 ms |
74108 KB |
Output is correct |
40 |
Correct |
186 ms |
74068 KB |
Output is correct |
41 |
Correct |
114 ms |
74064 KB |
Output is correct |
42 |
Correct |
185 ms |
64596 KB |
Output is correct |
43 |
Correct |
189 ms |
64700 KB |
Output is correct |
44 |
Correct |
318 ms |
134740 KB |
Output is correct |
45 |
Correct |
237 ms |
99164 KB |
Output is correct |
46 |
Correct |
95 ms |
61520 KB |
Output is correct |
47 |
Correct |
272 ms |
77728 KB |
Output is correct |
48 |
Correct |
171 ms |
70996 KB |
Output is correct |
49 |
Correct |
110 ms |
70936 KB |
Output is correct |
50 |
Correct |
263 ms |
79816 KB |
Output is correct |
51 |
Correct |
167 ms |
67324 KB |
Output is correct |
52 |
Correct |
269 ms |
77884 KB |
Output is correct |
53 |
Correct |
179 ms |
70924 KB |
Output is correct |
54 |
Correct |
236 ms |
86696 KB |
Output is correct |
55 |
Correct |
219 ms |
79300 KB |
Output is correct |
56 |
Correct |
187 ms |
81748 KB |
Output is correct |
57 |
Correct |
178 ms |
83280 KB |
Output is correct |
58 |
Correct |
212 ms |
83024 KB |
Output is correct |
59 |
Correct |
219 ms |
83092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
31320 KB |
Output is correct |
2 |
Correct |
6 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31408 KB |
Output is correct |
5 |
Correct |
13 ms |
32860 KB |
Output is correct |
6 |
Correct |
12 ms |
32460 KB |
Output is correct |
7 |
Correct |
10 ms |
32092 KB |
Output is correct |
8 |
Correct |
12 ms |
32860 KB |
Output is correct |
9 |
Correct |
11 ms |
32348 KB |
Output is correct |
10 |
Correct |
11 ms |
32236 KB |
Output is correct |
11 |
Correct |
9 ms |
32088 KB |
Output is correct |
12 |
Correct |
11 ms |
32456 KB |
Output is correct |
13 |
Correct |
9 ms |
32348 KB |
Output is correct |
14 |
Correct |
10 ms |
32092 KB |
Output is correct |
15 |
Correct |
10 ms |
32092 KB |
Output is correct |
16 |
Correct |
13 ms |
33116 KB |
Output is correct |
17 |
Correct |
11 ms |
32604 KB |
Output is correct |
18 |
Correct |
9 ms |
32356 KB |
Output is correct |
19 |
Correct |
12 ms |
32348 KB |
Output is correct |
20 |
Correct |
10 ms |
32348 KB |
Output is correct |
21 |
Correct |
9 ms |
32348 KB |
Output is correct |
22 |
Correct |
11 ms |
32604 KB |
Output is correct |
23 |
Correct |
10 ms |
32184 KB |
Output is correct |
24 |
Correct |
11 ms |
32352 KB |
Output is correct |
25 |
Correct |
9 ms |
32348 KB |
Output is correct |
26 |
Correct |
10 ms |
32792 KB |
Output is correct |
27 |
Correct |
11 ms |
32604 KB |
Output is correct |
28 |
Correct |
12 ms |
32604 KB |
Output is correct |
29 |
Correct |
12 ms |
32604 KB |
Output is correct |
30 |
Correct |
10 ms |
32600 KB |
Output is correct |
31 |
Correct |
11 ms |
32604 KB |
Output is correct |
32 |
Correct |
12 ms |
32972 KB |
Output is correct |
33 |
Correct |
380 ms |
109720 KB |
Output is correct |
34 |
Correct |
320 ms |
84844 KB |
Output is correct |
35 |
Correct |
348 ms |
106696 KB |
Output is correct |
36 |
Correct |
297 ms |
84620 KB |
Output is correct |
37 |
Correct |
224 ms |
65848 KB |
Output is correct |
38 |
Correct |
211 ms |
61268 KB |
Output is correct |
39 |
Correct |
198 ms |
74108 KB |
Output is correct |
40 |
Correct |
186 ms |
74068 KB |
Output is correct |
41 |
Correct |
114 ms |
74064 KB |
Output is correct |
42 |
Correct |
185 ms |
64596 KB |
Output is correct |
43 |
Correct |
189 ms |
64700 KB |
Output is correct |
44 |
Correct |
318 ms |
134740 KB |
Output is correct |
45 |
Correct |
237 ms |
99164 KB |
Output is correct |
46 |
Correct |
95 ms |
61520 KB |
Output is correct |
47 |
Correct |
272 ms |
77728 KB |
Output is correct |
48 |
Correct |
171 ms |
70996 KB |
Output is correct |
49 |
Correct |
110 ms |
70936 KB |
Output is correct |
50 |
Correct |
263 ms |
79816 KB |
Output is correct |
51 |
Correct |
167 ms |
67324 KB |
Output is correct |
52 |
Correct |
269 ms |
77884 KB |
Output is correct |
53 |
Correct |
179 ms |
70924 KB |
Output is correct |
54 |
Correct |
236 ms |
86696 KB |
Output is correct |
55 |
Correct |
219 ms |
79300 KB |
Output is correct |
56 |
Correct |
187 ms |
81748 KB |
Output is correct |
57 |
Correct |
178 ms |
83280 KB |
Output is correct |
58 |
Correct |
212 ms |
83024 KB |
Output is correct |
59 |
Correct |
219 ms |
83092 KB |
Output is correct |
60 |
Correct |
6 ms |
31320 KB |
Output is correct |
61 |
Correct |
6 ms |
31320 KB |
Output is correct |
62 |
Correct |
6 ms |
31324 KB |
Output is correct |
63 |
Correct |
6 ms |
31324 KB |
Output is correct |
64 |
Correct |
346 ms |
96084 KB |
Output is correct |
65 |
Correct |
297 ms |
79492 KB |
Output is correct |
66 |
Correct |
336 ms |
96888 KB |
Output is correct |
67 |
Correct |
306 ms |
79484 KB |
Output is correct |
68 |
Incorrect |
218 ms |
65624 KB |
Output isn't correct |
69 |
Halted |
0 ms |
0 KB |
- |