#ifdef local
#define debug(x) cout<<#x<<" "<<x<<endl;
#else
#define debug(x)
#endif
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pl;
const ll mod=1e9+7,lim=2e5+5,bitlim=15,inf=1e9;
vector<ll>adjl[lim];
vector<pl>costs[lim];
ll pwr[lim],cost[lim];
map<ll,ll> *mps[lim];
ll root[lim],nxt[lim];
ll vis[lim];
void dp(ll pos){
// debug(pos);
if(adjl[pos].size() == 0&&root[pos]==0){
mps[pos] = new map<ll,ll>();
mps[pos]->operator[](0)=0;
mps[pos]->operator[](pwr[pos] + 1) = cost[pos];
}
else{
ll maxsz=0,maxszidx=0;
for(int i=0;i<adjl[pos].size();i++){
ll u=adjl[pos][i];
dp(u);
if(mps[u]->size()>maxsz){
maxsz=mps[u]->size();
maxszidx=u;
}
}
if(maxszidx!=0)mps[pos] = mps[maxszidx];
else mps[pos] = new map<ll,ll>();
for(int i=0;i<adjl[pos].size();i++){
ll u=adjl[pos][i];
if(u==maxszidx)continue;
for(auto p:*mps[u]){
ll k,v;
tie(k,v)=p;
mps[pos]->operator[](k)+=v;
}
}
if(root[pos]==pos){
// cout<<"pos "<<pos<<" is a root\n";
for(pl pr:costs[pos]){
ll p,c;
tie(p,c)=pr;
mps[pos]->operator[](0) += c;
mps[pos]->operator[](p + 1) += c;
}
for(pl pr:costs[pos]){
ll p,c;
// cout<<"yeeting "<<p<<" "<<c<<"\n";
tie(p,c)=pr;
auto it=mps[pos]->upper_bound(p);
ll rem=c;
while(rem>0){
it--;
if(rem<=it->second)it->second-=rem,rem=0;
else rem-=it->second,it=mps[pos]->erase(it);
}
}
}
else{
mps[pos]->operator[](0) += cost[pos];
mps[pos]->operator[](pwr[pos] + 1) += cost[pos];
auto it=mps[pos]->upper_bound(pwr[pos]);
ll rem=cost[pos];
while(rem>0){
it--;
if(rem<=it->second)it->second-=rem,rem=0;
else rem-=it->second,it=mps[pos]->erase(it);
}
}
}
return;
}
ll dfs(ll pos,ll idx){
vis[pos]=idx;
if(vis[nxt[pos]]==0){
ll k = dfs(nxt[pos],idx);
if(k!=-1){
root[pos]=k;
costs[k].emplace_back(pwr[pos],cost[pos]);
if(k!=pos)return k;
}
return -1;
}
else if(vis[nxt[pos]]==idx){
root[nxt[pos]]=root[pos]=nxt[pos];
costs[nxt[pos]].emplace_back(pwr[pos],cost[pos]);
if(nxt[pos]==pos)return -1;
else return nxt[pos];
}
else return -1;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(NULL);
ll n;
cin>>n;
fill(root,root+n+1,-1);
for(int i=1;i<=n;i++){
ll a;
cin>>a>>pwr[i]>>cost[i];
nxt[i]=a;
}
for(int i=1;i<=n;i++){
if(vis[i]==0)dfs(i,i);
}
for(int i=1;i<=n;i++){
if(i!=nxt[i]&&root[i]==-1){
if(root[nxt[i]]!=-1){
adjl[root[nxt[i]]].push_back(i);
}
else adjl[nxt[i]].push_back(i);
}
}
ll ans=0;
/* for(int i=1;i<=n;i++){
cout<<i<<": "<<root[i]<<"\n";
}*/
for(int i=1;i<=n;i++){
if(root[i]==i){
// cout<<"rooti=i:"<<i<<"\n";
// for(auto x:costs[i]){
// cout<<x.first<<" "<<x.second<<"\n";
// }
dp(i);
// for(auto x:*mps[i]){
// cout<<x.first<<" "<<x.second<<"\n";
// }
ans+=mps[i]->begin()->second;
}
}
cout<<ans<<"\n";
/* for(int i=1;i<=n;i++){
cout<<i<<": ";
for(auto x:adjl[i])cout<<x<<" ";
cout<<"\n";
}*/
//dp(1);
//cout<<mps[1]->begin()->second<<"\n";
return 0;
}
/*
4
2 25 55
3 20 42
4 30 69
1 28 39
1 27 50
*/
Compilation message
worst_reporter2.cpp: In function 'void dp(ll)':
worst_reporter2.cpp:27:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=0;i<adjl[pos].size();i++){
| ~^~~~~~~~~~~~~~~~~
worst_reporter2.cpp:30:21: warning: comparison of integer expressions of different signedness: 'std::map<long long int, long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
30 | if(mps[u]->size()>maxsz){
| ~~~~~~~~~~~~~~^~~~~~
worst_reporter2.cpp:37:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i=0;i<adjl[pos].size();i++){
| ~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9716 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9804 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
11 ms |
11444 KB |
Output is correct |
6 |
Correct |
13 ms |
10956 KB |
Output is correct |
7 |
Correct |
10 ms |
10752 KB |
Output is correct |
8 |
Correct |
12 ms |
11392 KB |
Output is correct |
9 |
Correct |
10 ms |
11004 KB |
Output is correct |
10 |
Correct |
10 ms |
10752 KB |
Output is correct |
11 |
Correct |
9 ms |
10640 KB |
Output is correct |
12 |
Correct |
9 ms |
11056 KB |
Output is correct |
13 |
Correct |
9 ms |
11008 KB |
Output is correct |
14 |
Correct |
9 ms |
10572 KB |
Output is correct |
15 |
Correct |
9 ms |
10624 KB |
Output is correct |
16 |
Correct |
12 ms |
11752 KB |
Output is correct |
17 |
Correct |
11 ms |
11168 KB |
Output is correct |
18 |
Correct |
9 ms |
10660 KB |
Output is correct |
19 |
Correct |
10 ms |
11144 KB |
Output is correct |
20 |
Correct |
9 ms |
10956 KB |
Output is correct |
21 |
Correct |
9 ms |
10956 KB |
Output is correct |
22 |
Correct |
10 ms |
11340 KB |
Output is correct |
23 |
Correct |
9 ms |
11084 KB |
Output is correct |
24 |
Correct |
10 ms |
11212 KB |
Output is correct |
25 |
Correct |
9 ms |
10956 KB |
Output is correct |
26 |
Correct |
10 ms |
11324 KB |
Output is correct |
27 |
Correct |
10 ms |
11192 KB |
Output is correct |
28 |
Correct |
10 ms |
11264 KB |
Output is correct |
29 |
Correct |
10 ms |
11212 KB |
Output is correct |
30 |
Correct |
10 ms |
11052 KB |
Output is correct |
31 |
Correct |
10 ms |
11084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9716 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9804 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
11 ms |
11444 KB |
Output is correct |
6 |
Correct |
13 ms |
10956 KB |
Output is correct |
7 |
Correct |
10 ms |
10752 KB |
Output is correct |
8 |
Correct |
12 ms |
11392 KB |
Output is correct |
9 |
Correct |
10 ms |
11004 KB |
Output is correct |
10 |
Correct |
10 ms |
10752 KB |
Output is correct |
11 |
Correct |
9 ms |
10640 KB |
Output is correct |
12 |
Correct |
9 ms |
11056 KB |
Output is correct |
13 |
Correct |
9 ms |
11008 KB |
Output is correct |
14 |
Correct |
9 ms |
10572 KB |
Output is correct |
15 |
Correct |
9 ms |
10624 KB |
Output is correct |
16 |
Correct |
12 ms |
11752 KB |
Output is correct |
17 |
Correct |
11 ms |
11168 KB |
Output is correct |
18 |
Correct |
9 ms |
10660 KB |
Output is correct |
19 |
Correct |
10 ms |
11144 KB |
Output is correct |
20 |
Correct |
9 ms |
10956 KB |
Output is correct |
21 |
Correct |
9 ms |
10956 KB |
Output is correct |
22 |
Correct |
10 ms |
11340 KB |
Output is correct |
23 |
Correct |
9 ms |
11084 KB |
Output is correct |
24 |
Correct |
10 ms |
11212 KB |
Output is correct |
25 |
Correct |
9 ms |
10956 KB |
Output is correct |
26 |
Correct |
10 ms |
11324 KB |
Output is correct |
27 |
Correct |
10 ms |
11192 KB |
Output is correct |
28 |
Correct |
10 ms |
11264 KB |
Output is correct |
29 |
Correct |
10 ms |
11212 KB |
Output is correct |
30 |
Correct |
10 ms |
11052 KB |
Output is correct |
31 |
Correct |
10 ms |
11084 KB |
Output is correct |
32 |
Correct |
11 ms |
11392 KB |
Output is correct |
33 |
Correct |
345 ms |
89340 KB |
Output is correct |
34 |
Correct |
266 ms |
64440 KB |
Output is correct |
35 |
Correct |
333 ms |
86128 KB |
Output is correct |
36 |
Correct |
275 ms |
64508 KB |
Output is correct |
37 |
Correct |
201 ms |
46076 KB |
Output is correct |
38 |
Correct |
186 ms |
41768 KB |
Output is correct |
39 |
Correct |
164 ms |
59004 KB |
Output is correct |
40 |
Correct |
167 ms |
59076 KB |
Output is correct |
41 |
Correct |
119 ms |
58996 KB |
Output is correct |
42 |
Correct |
166 ms |
43700 KB |
Output is correct |
43 |
Correct |
149 ms |
43456 KB |
Output is correct |
44 |
Correct |
319 ms |
116680 KB |
Output is correct |
45 |
Correct |
232 ms |
81044 KB |
Output is correct |
46 |
Correct |
120 ms |
43332 KB |
Output is correct |
47 |
Correct |
258 ms |
66168 KB |
Output is correct |
48 |
Correct |
155 ms |
59068 KB |
Output is correct |
49 |
Correct |
128 ms |
59076 KB |
Output is correct |
50 |
Correct |
216 ms |
73104 KB |
Output is correct |
51 |
Correct |
138 ms |
60604 KB |
Output is correct |
52 |
Correct |
219 ms |
66988 KB |
Output is correct |
53 |
Correct |
143 ms |
59868 KB |
Output is correct |
54 |
Correct |
214 ms |
71564 KB |
Output is correct |
55 |
Correct |
225 ms |
69024 KB |
Output is correct |
56 |
Correct |
191 ms |
68988 KB |
Output is correct |
57 |
Correct |
185 ms |
69756 KB |
Output is correct |
58 |
Correct |
206 ms |
61948 KB |
Output is correct |
59 |
Correct |
203 ms |
61968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9716 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9804 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
11 ms |
11444 KB |
Output is correct |
6 |
Correct |
13 ms |
10956 KB |
Output is correct |
7 |
Correct |
10 ms |
10752 KB |
Output is correct |
8 |
Correct |
12 ms |
11392 KB |
Output is correct |
9 |
Correct |
10 ms |
11004 KB |
Output is correct |
10 |
Correct |
10 ms |
10752 KB |
Output is correct |
11 |
Correct |
9 ms |
10640 KB |
Output is correct |
12 |
Correct |
9 ms |
11056 KB |
Output is correct |
13 |
Correct |
9 ms |
11008 KB |
Output is correct |
14 |
Correct |
9 ms |
10572 KB |
Output is correct |
15 |
Correct |
9 ms |
10624 KB |
Output is correct |
16 |
Correct |
12 ms |
11752 KB |
Output is correct |
17 |
Correct |
11 ms |
11168 KB |
Output is correct |
18 |
Correct |
9 ms |
10660 KB |
Output is correct |
19 |
Correct |
10 ms |
11144 KB |
Output is correct |
20 |
Correct |
9 ms |
10956 KB |
Output is correct |
21 |
Correct |
9 ms |
10956 KB |
Output is correct |
22 |
Correct |
10 ms |
11340 KB |
Output is correct |
23 |
Correct |
9 ms |
11084 KB |
Output is correct |
24 |
Correct |
10 ms |
11212 KB |
Output is correct |
25 |
Correct |
9 ms |
10956 KB |
Output is correct |
26 |
Correct |
10 ms |
11324 KB |
Output is correct |
27 |
Correct |
10 ms |
11192 KB |
Output is correct |
28 |
Correct |
10 ms |
11264 KB |
Output is correct |
29 |
Correct |
10 ms |
11212 KB |
Output is correct |
30 |
Correct |
10 ms |
11052 KB |
Output is correct |
31 |
Correct |
10 ms |
11084 KB |
Output is correct |
32 |
Correct |
11 ms |
11392 KB |
Output is correct |
33 |
Correct |
345 ms |
89340 KB |
Output is correct |
34 |
Correct |
266 ms |
64440 KB |
Output is correct |
35 |
Correct |
333 ms |
86128 KB |
Output is correct |
36 |
Correct |
275 ms |
64508 KB |
Output is correct |
37 |
Correct |
201 ms |
46076 KB |
Output is correct |
38 |
Correct |
186 ms |
41768 KB |
Output is correct |
39 |
Correct |
164 ms |
59004 KB |
Output is correct |
40 |
Correct |
167 ms |
59076 KB |
Output is correct |
41 |
Correct |
119 ms |
58996 KB |
Output is correct |
42 |
Correct |
166 ms |
43700 KB |
Output is correct |
43 |
Correct |
149 ms |
43456 KB |
Output is correct |
44 |
Correct |
319 ms |
116680 KB |
Output is correct |
45 |
Correct |
232 ms |
81044 KB |
Output is correct |
46 |
Correct |
120 ms |
43332 KB |
Output is correct |
47 |
Correct |
258 ms |
66168 KB |
Output is correct |
48 |
Correct |
155 ms |
59068 KB |
Output is correct |
49 |
Correct |
128 ms |
59076 KB |
Output is correct |
50 |
Correct |
216 ms |
73104 KB |
Output is correct |
51 |
Correct |
138 ms |
60604 KB |
Output is correct |
52 |
Correct |
219 ms |
66988 KB |
Output is correct |
53 |
Correct |
143 ms |
59868 KB |
Output is correct |
54 |
Correct |
214 ms |
71564 KB |
Output is correct |
55 |
Correct |
225 ms |
69024 KB |
Output is correct |
56 |
Correct |
191 ms |
68988 KB |
Output is correct |
57 |
Correct |
185 ms |
69756 KB |
Output is correct |
58 |
Correct |
206 ms |
61948 KB |
Output is correct |
59 |
Correct |
203 ms |
61968 KB |
Output is correct |
60 |
Correct |
5 ms |
9676 KB |
Output is correct |
61 |
Correct |
6 ms |
9712 KB |
Output is correct |
62 |
Correct |
6 ms |
9676 KB |
Output is correct |
63 |
Correct |
5 ms |
9716 KB |
Output is correct |
64 |
Correct |
339 ms |
75412 KB |
Output is correct |
65 |
Correct |
268 ms |
58820 KB |
Output is correct |
66 |
Correct |
332 ms |
74772 KB |
Output is correct |
67 |
Correct |
265 ms |
58684 KB |
Output is correct |
68 |
Correct |
215 ms |
45536 KB |
Output is correct |
69 |
Correct |
202 ms |
42308 KB |
Output is correct |
70 |
Correct |
260 ms |
40840 KB |
Output is correct |
71 |
Correct |
134 ms |
32060 KB |
Output is correct |
72 |
Correct |
300 ms |
47704 KB |
Output is correct |
73 |
Correct |
134 ms |
36252 KB |
Output is correct |
74 |
Correct |
269 ms |
62520 KB |
Output is correct |
75 |
Correct |
147 ms |
50220 KB |
Output is correct |
76 |
Correct |
124 ms |
50024 KB |
Output is correct |
77 |
Correct |
262 ms |
62604 KB |
Output is correct |
78 |
Correct |
141 ms |
50044 KB |
Output is correct |
79 |
Correct |
318 ms |
70048 KB |
Output is correct |
80 |
Correct |
217 ms |
53052 KB |
Output is correct |
81 |
Correct |
157 ms |
43764 KB |
Output is correct |
82 |
Correct |
203 ms |
47920 KB |
Output is correct |
83 |
Correct |
132 ms |
67936 KB |
Output is correct |
84 |
Correct |
256 ms |
56528 KB |
Output is correct |
85 |
Correct |
275 ms |
56564 KB |
Output is correct |
86 |
Correct |
258 ms |
53308 KB |
Output is correct |
87 |
Correct |
263 ms |
56532 KB |
Output is correct |
88 |
Correct |
261 ms |
56568 KB |
Output is correct |