#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=2e5+5;
int N;
int A[MX],H[MX],C[MX];
vector<int> adj[MX];
multiset<pair<ll,ll>> dp[MX];
int par[MX];
int find(int v) {
return par[v]==v?v:par[v]=find(par[v]);
}
void merge(int u, int v) {
u=find(u),v=find(v);
if(u==v) return;
if(dp[u].size()>dp[v].size()) swap(u,v);
for(auto [pos,val]:dp[u]) {
dp[v].insert({pos,val});
}
par[u]=v;
dp[u].clear();
}
void dfs(int v) {
int prv=-1;
for(auto u:adj[v]) {
dfs(u);
if(prv!=-1)
merge(u,prv);
prv=u;
}
if(prv!=-1) merge(v,prv);
int pv=find(v);
auto it=dp[pv].lower_bound(make_pair(H[v],0));
if(it==dp[pv].begin()) {
dp[pv].insert(make_pair(H[v],C[v]));
} else {
it--;
ll cur=C[v];
while(true) {
if(cur>=it->second) {
cur-=it->second;
it=dp[pv].erase(it);
if(it==dp[pv].begin()) break;
it--;
} else {
auto [p,q]=*it;
dp[pv].erase(it);
dp[pv].insert({p,q-cur});
cur=0;
break;
}
}
dp[pv].insert(make_pair(H[v],C[v]));
}
}
int onstk[MX],degs[MX],head[MX];
vector<int> stk;
void dfs0(int v) {
onstk[v]=1;
stk.push_back(v);
if(onstk[A[v]]==0) {
dfs0(A[v]);
} else if(onstk[A[v]]==1) {
vector<int> cyc;
for(int i=stk.size()-1;i>=0;i--) {
cyc.push_back(stk[i]);
if(stk[i]==A[v]) break;
}
sort(cyc.begin(),cyc.end(),[&](int i, int j){
return H[i]>H[j];
});
for(int i=0;i+1<cyc.size();i++) {
head[cyc[i+1]]=cyc[0];
A[cyc[i+1]]=cyc[i];
}
head[cyc[0]]=cyc[0];
A[cyc[0]]=cyc[0];
}
stk.pop_back();
onstk[v]=2;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N;
ll sum=0;
for(int i=1;i<=N;i++) {
cin>>A[i]>>H[i]>>C[i];
sum+=C[i];
}
for(int i=1;i<=N;i++) {
head[i]=i;
if(onstk[i]==2) continue;
dfs0(i);
}
// for(int i=1;i<=N;i++) {
// cout<<A[i]<<" -> "<<i<<'\n';
// cout<<head[A[i]]<<" "<<head[i]<<'\n';
// }
for(int i=1;i<=N;i++) {
if(A[i]!=i) {
if(head[A[i]]!=head[i]) {
adj[head[A[i]]].push_back(head[i]);
degs[head[i]]+=1;
} else {
adj[A[i]].push_back(i);
degs[i]+=1;
}
}
}
for(int i=1;i<=N;i++) par[i]=i;
ll ans=0;
for(int i=1;i<=N;i++) {
if(degs[i]==0) {
dfs(i);
for(auto [pos,val]:dp[find(i)])
ans+=val;
}
}
cout<<sum-ans<<'\n';
}
Compilation message
worst_reporter2.cpp: In function 'void dfs0(int)':
worst_reporter2.cpp:91:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i=0;i+1<cyc.size();i++) {
| ~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
9 ms |
17264 KB |
Output is correct |
6 |
Correct |
7 ms |
17240 KB |
Output is correct |
7 |
Correct |
7 ms |
17240 KB |
Output is correct |
8 |
Correct |
7 ms |
17240 KB |
Output is correct |
9 |
Correct |
7 ms |
17244 KB |
Output is correct |
10 |
Correct |
7 ms |
17336 KB |
Output is correct |
11 |
Correct |
7 ms |
17244 KB |
Output is correct |
12 |
Correct |
8 ms |
17500 KB |
Output is correct |
13 |
Correct |
6 ms |
17660 KB |
Output is correct |
14 |
Correct |
8 ms |
17444 KB |
Output is correct |
15 |
Correct |
6 ms |
17240 KB |
Output is correct |
16 |
Correct |
7 ms |
17244 KB |
Output is correct |
17 |
Correct |
9 ms |
17244 KB |
Output is correct |
18 |
Correct |
8 ms |
17244 KB |
Output is correct |
19 |
Correct |
6 ms |
17340 KB |
Output is correct |
20 |
Correct |
6 ms |
17500 KB |
Output is correct |
21 |
Correct |
6 ms |
17500 KB |
Output is correct |
22 |
Correct |
5 ms |
17244 KB |
Output is correct |
23 |
Correct |
7 ms |
17104 KB |
Output is correct |
24 |
Correct |
6 ms |
17496 KB |
Output is correct |
25 |
Correct |
6 ms |
17500 KB |
Output is correct |
26 |
Correct |
5 ms |
17748 KB |
Output is correct |
27 |
Correct |
6 ms |
17500 KB |
Output is correct |
28 |
Correct |
6 ms |
17500 KB |
Output is correct |
29 |
Correct |
6 ms |
17756 KB |
Output is correct |
30 |
Correct |
7 ms |
17756 KB |
Output is correct |
31 |
Correct |
7 ms |
18012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
9 ms |
17264 KB |
Output is correct |
6 |
Correct |
7 ms |
17240 KB |
Output is correct |
7 |
Correct |
7 ms |
17240 KB |
Output is correct |
8 |
Correct |
7 ms |
17240 KB |
Output is correct |
9 |
Correct |
7 ms |
17244 KB |
Output is correct |
10 |
Correct |
7 ms |
17336 KB |
Output is correct |
11 |
Correct |
7 ms |
17244 KB |
Output is correct |
12 |
Correct |
8 ms |
17500 KB |
Output is correct |
13 |
Correct |
6 ms |
17660 KB |
Output is correct |
14 |
Correct |
8 ms |
17444 KB |
Output is correct |
15 |
Correct |
6 ms |
17240 KB |
Output is correct |
16 |
Correct |
7 ms |
17244 KB |
Output is correct |
17 |
Correct |
9 ms |
17244 KB |
Output is correct |
18 |
Correct |
8 ms |
17244 KB |
Output is correct |
19 |
Correct |
6 ms |
17340 KB |
Output is correct |
20 |
Correct |
6 ms |
17500 KB |
Output is correct |
21 |
Correct |
6 ms |
17500 KB |
Output is correct |
22 |
Correct |
5 ms |
17244 KB |
Output is correct |
23 |
Correct |
7 ms |
17104 KB |
Output is correct |
24 |
Correct |
6 ms |
17496 KB |
Output is correct |
25 |
Correct |
6 ms |
17500 KB |
Output is correct |
26 |
Correct |
5 ms |
17748 KB |
Output is correct |
27 |
Correct |
6 ms |
17500 KB |
Output is correct |
28 |
Correct |
6 ms |
17500 KB |
Output is correct |
29 |
Correct |
6 ms |
17756 KB |
Output is correct |
30 |
Correct |
7 ms |
17756 KB |
Output is correct |
31 |
Correct |
7 ms |
18012 KB |
Output is correct |
32 |
Correct |
7 ms |
17240 KB |
Output is correct |
33 |
Correct |
219 ms |
35568 KB |
Output is correct |
34 |
Correct |
222 ms |
34292 KB |
Output is correct |
35 |
Correct |
233 ms |
33548 KB |
Output is correct |
36 |
Correct |
215 ms |
34720 KB |
Output is correct |
37 |
Correct |
224 ms |
36836 KB |
Output is correct |
38 |
Correct |
244 ms |
35700 KB |
Output is correct |
39 |
Correct |
131 ms |
51280 KB |
Output is correct |
40 |
Correct |
135 ms |
51348 KB |
Output is correct |
41 |
Correct |
139 ms |
54308 KB |
Output is correct |
42 |
Correct |
123 ms |
38860 KB |
Output is correct |
43 |
Correct |
136 ms |
38992 KB |
Output is correct |
44 |
Correct |
207 ms |
35404 KB |
Output is correct |
45 |
Correct |
209 ms |
35260 KB |
Output is correct |
46 |
Correct |
273 ms |
39760 KB |
Output is correct |
47 |
Correct |
158 ms |
42516 KB |
Output is correct |
48 |
Correct |
181 ms |
46416 KB |
Output is correct |
49 |
Correct |
177 ms |
46932 KB |
Output is correct |
50 |
Correct |
140 ms |
33228 KB |
Output is correct |
51 |
Correct |
145 ms |
33264 KB |
Output is correct |
52 |
Correct |
159 ms |
43212 KB |
Output is correct |
53 |
Correct |
201 ms |
47236 KB |
Output is correct |
54 |
Correct |
80 ms |
51076 KB |
Output is correct |
55 |
Correct |
139 ms |
45052 KB |
Output is correct |
56 |
Correct |
120 ms |
53252 KB |
Output is correct |
57 |
Correct |
152 ms |
57096 KB |
Output is correct |
58 |
Correct |
150 ms |
57420 KB |
Output is correct |
59 |
Correct |
150 ms |
57556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
9 ms |
17264 KB |
Output is correct |
6 |
Correct |
7 ms |
17240 KB |
Output is correct |
7 |
Correct |
7 ms |
17240 KB |
Output is correct |
8 |
Correct |
7 ms |
17240 KB |
Output is correct |
9 |
Correct |
7 ms |
17244 KB |
Output is correct |
10 |
Correct |
7 ms |
17336 KB |
Output is correct |
11 |
Correct |
7 ms |
17244 KB |
Output is correct |
12 |
Correct |
8 ms |
17500 KB |
Output is correct |
13 |
Correct |
6 ms |
17660 KB |
Output is correct |
14 |
Correct |
8 ms |
17444 KB |
Output is correct |
15 |
Correct |
6 ms |
17240 KB |
Output is correct |
16 |
Correct |
7 ms |
17244 KB |
Output is correct |
17 |
Correct |
9 ms |
17244 KB |
Output is correct |
18 |
Correct |
8 ms |
17244 KB |
Output is correct |
19 |
Correct |
6 ms |
17340 KB |
Output is correct |
20 |
Correct |
6 ms |
17500 KB |
Output is correct |
21 |
Correct |
6 ms |
17500 KB |
Output is correct |
22 |
Correct |
5 ms |
17244 KB |
Output is correct |
23 |
Correct |
7 ms |
17104 KB |
Output is correct |
24 |
Correct |
6 ms |
17496 KB |
Output is correct |
25 |
Correct |
6 ms |
17500 KB |
Output is correct |
26 |
Correct |
5 ms |
17748 KB |
Output is correct |
27 |
Correct |
6 ms |
17500 KB |
Output is correct |
28 |
Correct |
6 ms |
17500 KB |
Output is correct |
29 |
Correct |
6 ms |
17756 KB |
Output is correct |
30 |
Correct |
7 ms |
17756 KB |
Output is correct |
31 |
Correct |
7 ms |
18012 KB |
Output is correct |
32 |
Correct |
7 ms |
17240 KB |
Output is correct |
33 |
Correct |
219 ms |
35568 KB |
Output is correct |
34 |
Correct |
222 ms |
34292 KB |
Output is correct |
35 |
Correct |
233 ms |
33548 KB |
Output is correct |
36 |
Correct |
215 ms |
34720 KB |
Output is correct |
37 |
Correct |
224 ms |
36836 KB |
Output is correct |
38 |
Correct |
244 ms |
35700 KB |
Output is correct |
39 |
Correct |
131 ms |
51280 KB |
Output is correct |
40 |
Correct |
135 ms |
51348 KB |
Output is correct |
41 |
Correct |
139 ms |
54308 KB |
Output is correct |
42 |
Correct |
123 ms |
38860 KB |
Output is correct |
43 |
Correct |
136 ms |
38992 KB |
Output is correct |
44 |
Correct |
207 ms |
35404 KB |
Output is correct |
45 |
Correct |
209 ms |
35260 KB |
Output is correct |
46 |
Correct |
273 ms |
39760 KB |
Output is correct |
47 |
Correct |
158 ms |
42516 KB |
Output is correct |
48 |
Correct |
181 ms |
46416 KB |
Output is correct |
49 |
Correct |
177 ms |
46932 KB |
Output is correct |
50 |
Correct |
140 ms |
33228 KB |
Output is correct |
51 |
Correct |
145 ms |
33264 KB |
Output is correct |
52 |
Correct |
159 ms |
43212 KB |
Output is correct |
53 |
Correct |
201 ms |
47236 KB |
Output is correct |
54 |
Correct |
80 ms |
51076 KB |
Output is correct |
55 |
Correct |
139 ms |
45052 KB |
Output is correct |
56 |
Correct |
120 ms |
53252 KB |
Output is correct |
57 |
Correct |
152 ms |
57096 KB |
Output is correct |
58 |
Correct |
150 ms |
57420 KB |
Output is correct |
59 |
Correct |
150 ms |
57556 KB |
Output is correct |
60 |
Correct |
4 ms |
16728 KB |
Output is correct |
61 |
Correct |
3 ms |
16744 KB |
Output is correct |
62 |
Correct |
3 ms |
16732 KB |
Output is correct |
63 |
Correct |
4 ms |
16732 KB |
Output is correct |
64 |
Incorrect |
235 ms |
32600 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |