#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 |
8 ms |
31580 KB |
Output is correct |
2 |
Correct |
7 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31324 KB |
Output is correct |
5 |
Correct |
12 ms |
32860 KB |
Output is correct |
6 |
Correct |
11 ms |
32348 KB |
Output is correct |
7 |
Correct |
12 ms |
32092 KB |
Output is correct |
8 |
Correct |
12 ms |
32856 KB |
Output is correct |
9 |
Correct |
11 ms |
32440 KB |
Output is correct |
10 |
Correct |
10 ms |
32092 KB |
Output is correct |
11 |
Correct |
9 ms |
31956 KB |
Output is correct |
12 |
Correct |
11 ms |
32348 KB |
Output is correct |
13 |
Correct |
10 ms |
32344 KB |
Output is correct |
14 |
Correct |
11 ms |
32092 KB |
Output is correct |
15 |
Correct |
10 ms |
32232 KB |
Output is correct |
16 |
Correct |
13 ms |
33112 KB |
Output is correct |
17 |
Correct |
11 ms |
32856 KB |
Output is correct |
18 |
Correct |
8 ms |
32092 KB |
Output is correct |
19 |
Correct |
10 ms |
32392 KB |
Output is correct |
20 |
Correct |
10 ms |
32268 KB |
Output is correct |
21 |
Correct |
9 ms |
32396 KB |
Output is correct |
22 |
Correct |
10 ms |
32604 KB |
Output is correct |
23 |
Correct |
10 ms |
32092 KB |
Output is correct |
24 |
Correct |
14 ms |
32860 KB |
Output is correct |
25 |
Correct |
10 ms |
32348 KB |
Output is correct |
26 |
Correct |
10 ms |
32604 KB |
Output is correct |
27 |
Correct |
14 ms |
32348 KB |
Output is correct |
28 |
Correct |
11 ms |
32604 KB |
Output is correct |
29 |
Correct |
11 ms |
32600 KB |
Output is correct |
30 |
Correct |
11 ms |
32600 KB |
Output is correct |
31 |
Correct |
11 ms |
32604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31580 KB |
Output is correct |
2 |
Correct |
7 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31324 KB |
Output is correct |
5 |
Correct |
12 ms |
32860 KB |
Output is correct |
6 |
Correct |
11 ms |
32348 KB |
Output is correct |
7 |
Correct |
12 ms |
32092 KB |
Output is correct |
8 |
Correct |
12 ms |
32856 KB |
Output is correct |
9 |
Correct |
11 ms |
32440 KB |
Output is correct |
10 |
Correct |
10 ms |
32092 KB |
Output is correct |
11 |
Correct |
9 ms |
31956 KB |
Output is correct |
12 |
Correct |
11 ms |
32348 KB |
Output is correct |
13 |
Correct |
10 ms |
32344 KB |
Output is correct |
14 |
Correct |
11 ms |
32092 KB |
Output is correct |
15 |
Correct |
10 ms |
32232 KB |
Output is correct |
16 |
Correct |
13 ms |
33112 KB |
Output is correct |
17 |
Correct |
11 ms |
32856 KB |
Output is correct |
18 |
Correct |
8 ms |
32092 KB |
Output is correct |
19 |
Correct |
10 ms |
32392 KB |
Output is correct |
20 |
Correct |
10 ms |
32268 KB |
Output is correct |
21 |
Correct |
9 ms |
32396 KB |
Output is correct |
22 |
Correct |
10 ms |
32604 KB |
Output is correct |
23 |
Correct |
10 ms |
32092 KB |
Output is correct |
24 |
Correct |
14 ms |
32860 KB |
Output is correct |
25 |
Correct |
10 ms |
32348 KB |
Output is correct |
26 |
Correct |
10 ms |
32604 KB |
Output is correct |
27 |
Correct |
14 ms |
32348 KB |
Output is correct |
28 |
Correct |
11 ms |
32604 KB |
Output is correct |
29 |
Correct |
11 ms |
32600 KB |
Output is correct |
30 |
Correct |
11 ms |
32600 KB |
Output is correct |
31 |
Correct |
11 ms |
32604 KB |
Output is correct |
32 |
Correct |
12 ms |
32856 KB |
Output is correct |
33 |
Correct |
385 ms |
112000 KB |
Output is correct |
34 |
Correct |
341 ms |
86828 KB |
Output is correct |
35 |
Correct |
367 ms |
108960 KB |
Output is correct |
36 |
Correct |
323 ms |
86868 KB |
Output is correct |
37 |
Correct |
225 ms |
68176 KB |
Output is correct |
38 |
Correct |
203 ms |
63316 KB |
Output is correct |
39 |
Correct |
239 ms |
76388 KB |
Output is correct |
40 |
Correct |
194 ms |
76424 KB |
Output is correct |
41 |
Correct |
117 ms |
76396 KB |
Output is correct |
42 |
Correct |
189 ms |
66940 KB |
Output is correct |
43 |
Correct |
187 ms |
67280 KB |
Output is correct |
44 |
Correct |
327 ms |
136784 KB |
Output is correct |
45 |
Correct |
258 ms |
101228 KB |
Output is correct |
46 |
Correct |
98 ms |
63572 KB |
Output is correct |
47 |
Correct |
271 ms |
79880 KB |
Output is correct |
48 |
Correct |
176 ms |
73040 KB |
Output is correct |
49 |
Correct |
116 ms |
72980 KB |
Output is correct |
50 |
Correct |
283 ms |
81740 KB |
Output is correct |
51 |
Correct |
169 ms |
69204 KB |
Output is correct |
52 |
Correct |
293 ms |
80244 KB |
Output is correct |
53 |
Correct |
185 ms |
73288 KB |
Output is correct |
54 |
Correct |
221 ms |
89100 KB |
Output is correct |
55 |
Correct |
226 ms |
81260 KB |
Output is correct |
56 |
Correct |
224 ms |
83640 KB |
Output is correct |
57 |
Correct |
207 ms |
85024 KB |
Output is correct |
58 |
Correct |
226 ms |
84896 KB |
Output is correct |
59 |
Correct |
223 ms |
85056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31580 KB |
Output is correct |
2 |
Correct |
7 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31324 KB |
Output is correct |
5 |
Correct |
12 ms |
32860 KB |
Output is correct |
6 |
Correct |
11 ms |
32348 KB |
Output is correct |
7 |
Correct |
12 ms |
32092 KB |
Output is correct |
8 |
Correct |
12 ms |
32856 KB |
Output is correct |
9 |
Correct |
11 ms |
32440 KB |
Output is correct |
10 |
Correct |
10 ms |
32092 KB |
Output is correct |
11 |
Correct |
9 ms |
31956 KB |
Output is correct |
12 |
Correct |
11 ms |
32348 KB |
Output is correct |
13 |
Correct |
10 ms |
32344 KB |
Output is correct |
14 |
Correct |
11 ms |
32092 KB |
Output is correct |
15 |
Correct |
10 ms |
32232 KB |
Output is correct |
16 |
Correct |
13 ms |
33112 KB |
Output is correct |
17 |
Correct |
11 ms |
32856 KB |
Output is correct |
18 |
Correct |
8 ms |
32092 KB |
Output is correct |
19 |
Correct |
10 ms |
32392 KB |
Output is correct |
20 |
Correct |
10 ms |
32268 KB |
Output is correct |
21 |
Correct |
9 ms |
32396 KB |
Output is correct |
22 |
Correct |
10 ms |
32604 KB |
Output is correct |
23 |
Correct |
10 ms |
32092 KB |
Output is correct |
24 |
Correct |
14 ms |
32860 KB |
Output is correct |
25 |
Correct |
10 ms |
32348 KB |
Output is correct |
26 |
Correct |
10 ms |
32604 KB |
Output is correct |
27 |
Correct |
14 ms |
32348 KB |
Output is correct |
28 |
Correct |
11 ms |
32604 KB |
Output is correct |
29 |
Correct |
11 ms |
32600 KB |
Output is correct |
30 |
Correct |
11 ms |
32600 KB |
Output is correct |
31 |
Correct |
11 ms |
32604 KB |
Output is correct |
32 |
Correct |
12 ms |
32856 KB |
Output is correct |
33 |
Correct |
385 ms |
112000 KB |
Output is correct |
34 |
Correct |
341 ms |
86828 KB |
Output is correct |
35 |
Correct |
367 ms |
108960 KB |
Output is correct |
36 |
Correct |
323 ms |
86868 KB |
Output is correct |
37 |
Correct |
225 ms |
68176 KB |
Output is correct |
38 |
Correct |
203 ms |
63316 KB |
Output is correct |
39 |
Correct |
239 ms |
76388 KB |
Output is correct |
40 |
Correct |
194 ms |
76424 KB |
Output is correct |
41 |
Correct |
117 ms |
76396 KB |
Output is correct |
42 |
Correct |
189 ms |
66940 KB |
Output is correct |
43 |
Correct |
187 ms |
67280 KB |
Output is correct |
44 |
Correct |
327 ms |
136784 KB |
Output is correct |
45 |
Correct |
258 ms |
101228 KB |
Output is correct |
46 |
Correct |
98 ms |
63572 KB |
Output is correct |
47 |
Correct |
271 ms |
79880 KB |
Output is correct |
48 |
Correct |
176 ms |
73040 KB |
Output is correct |
49 |
Correct |
116 ms |
72980 KB |
Output is correct |
50 |
Correct |
283 ms |
81740 KB |
Output is correct |
51 |
Correct |
169 ms |
69204 KB |
Output is correct |
52 |
Correct |
293 ms |
80244 KB |
Output is correct |
53 |
Correct |
185 ms |
73288 KB |
Output is correct |
54 |
Correct |
221 ms |
89100 KB |
Output is correct |
55 |
Correct |
226 ms |
81260 KB |
Output is correct |
56 |
Correct |
224 ms |
83640 KB |
Output is correct |
57 |
Correct |
207 ms |
85024 KB |
Output is correct |
58 |
Correct |
226 ms |
84896 KB |
Output is correct |
59 |
Correct |
223 ms |
85056 KB |
Output is correct |
60 |
Incorrect |
6 ms |
31320 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |