#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=2e5+7;
vector<int>V[LIM];
ll A[LIM], T[LIM], C[LIM], odw[LIM], cykl[LIM], root[LIM], ile[LIM];
void DFS(int x) {
ile[x]=1;
for(auto i : V[x]) {
DFS(i);
ile[x]+=ile[i];
}
rep(i, V[x].size()) if(ile[V[x][i]]>ile[V[x][0]]) swap(V[x][i], V[x][0]);
}
void DFS2(int x, multiset<pair<ll,ll>>&S) {
if(V[x].size()) DFS2(V[x][0], S);
for(int i=1; i<V[x].size(); ++i) {
multiset<pair<ll,ll>>P;
DFS2(V[x][i], P);
for(auto j : P) S.insert(j);
}
ll a=C[x];
while(a) {
auto it=S.lower_bound({T[x], -1});
if(it==S.begin()) break;
--it;
auto p=*it;
S.erase(S.find(p));
if(p.nd<=a) {
a-=p.nd;
continue;
}
p.nd-=a;
a=0;
S.insert(p);
}
S.insert({T[x], C[x]});
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
rep(i, n) {
cin >> A[i] >> T[i] >> C[i];
--A[i];
}
rep(i, n) if(!odw[i]) {
int p=i;
while(!odw[p]) {
odw[p]=i+1;
p=A[p];
}
if(odw[p]!=i+1) continue;
int x=A[p];
vector<pair<ll,ll>>P;
while(x!=p) {
P.pb({T[x], C[x]});
cykl[x]=1;
root[x]=p;
x=A[x];
}
P.pb({T[x], C[x]});
cykl[x]=1;
root[x]=p;
sort(all(P));
x=p;
for(auto j : P) {
T[x]=j.st;
C[x]=j.nd;
if(A[x]==p) A[x]=x;
x=A[x];
}
}
rep(i, n) if(!cykl[i] && cykl[A[i]]) A[i]=root[A[i]];
rep(i, n) if(A[i]!=i) V[A[i]].pb(i);
ll sum=0;
rep(i, n) sum+=C[i];
rep(i, n) if(A[i]==i) {
DFS(i);
multiset<pair<ll,ll>>S;
DFS2(i, S);
for(auto j : S) sum-=j.nd;
}
cout << sum << '\n';
}
Compilation message
worst_reporter2.cpp: In function 'void DFS(int)':
worst_reporter2.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
worst_reporter2.cpp:19:3: note: in expansion of macro 'rep'
19 | rep(i, V[x].size()) if(ile[V[x][i]]>ile[V[x][0]]) swap(V[x][i], V[x][0]);
| ^~~
worst_reporter2.cpp: In function 'void DFS2(int, std::multiset<std::pair<long long int, long long int> >&)':
worst_reporter2.cpp:23:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=1; i<V[x].size(); ++i) {
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
15708 KB |
Output is correct |
2 |
Correct |
2 ms |
15708 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
2 ms |
15708 KB |
Output is correct |
5 |
Correct |
5 ms |
16328 KB |
Output is correct |
6 |
Correct |
5 ms |
16220 KB |
Output is correct |
7 |
Correct |
5 ms |
16220 KB |
Output is correct |
8 |
Correct |
5 ms |
16220 KB |
Output is correct |
9 |
Correct |
5 ms |
16188 KB |
Output is correct |
10 |
Correct |
6 ms |
16220 KB |
Output is correct |
11 |
Correct |
6 ms |
16268 KB |
Output is correct |
12 |
Correct |
5 ms |
16732 KB |
Output is correct |
13 |
Correct |
6 ms |
16988 KB |
Output is correct |
14 |
Correct |
6 ms |
16476 KB |
Output is correct |
15 |
Correct |
7 ms |
16476 KB |
Output is correct |
16 |
Correct |
6 ms |
16220 KB |
Output is correct |
17 |
Correct |
6 ms |
16220 KB |
Output is correct |
18 |
Correct |
7 ms |
16476 KB |
Output is correct |
19 |
Correct |
5 ms |
16632 KB |
Output is correct |
20 |
Correct |
5 ms |
16472 KB |
Output is correct |
21 |
Correct |
4 ms |
16732 KB |
Output is correct |
22 |
Correct |
5 ms |
16220 KB |
Output is correct |
23 |
Correct |
4 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16476 KB |
Output is correct |
25 |
Correct |
5 ms |
16732 KB |
Output is correct |
26 |
Correct |
4 ms |
16732 KB |
Output is correct |
27 |
Correct |
4 ms |
16476 KB |
Output is correct |
28 |
Correct |
4 ms |
16732 KB |
Output is correct |
29 |
Correct |
4 ms |
16988 KB |
Output is correct |
30 |
Correct |
5 ms |
16988 KB |
Output is correct |
31 |
Correct |
5 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
15708 KB |
Output is correct |
2 |
Correct |
2 ms |
15708 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
2 ms |
15708 KB |
Output is correct |
5 |
Correct |
5 ms |
16328 KB |
Output is correct |
6 |
Correct |
5 ms |
16220 KB |
Output is correct |
7 |
Correct |
5 ms |
16220 KB |
Output is correct |
8 |
Correct |
5 ms |
16220 KB |
Output is correct |
9 |
Correct |
5 ms |
16188 KB |
Output is correct |
10 |
Correct |
6 ms |
16220 KB |
Output is correct |
11 |
Correct |
6 ms |
16268 KB |
Output is correct |
12 |
Correct |
5 ms |
16732 KB |
Output is correct |
13 |
Correct |
6 ms |
16988 KB |
Output is correct |
14 |
Correct |
6 ms |
16476 KB |
Output is correct |
15 |
Correct |
7 ms |
16476 KB |
Output is correct |
16 |
Correct |
6 ms |
16220 KB |
Output is correct |
17 |
Correct |
6 ms |
16220 KB |
Output is correct |
18 |
Correct |
7 ms |
16476 KB |
Output is correct |
19 |
Correct |
5 ms |
16632 KB |
Output is correct |
20 |
Correct |
5 ms |
16472 KB |
Output is correct |
21 |
Correct |
4 ms |
16732 KB |
Output is correct |
22 |
Correct |
5 ms |
16220 KB |
Output is correct |
23 |
Correct |
4 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16476 KB |
Output is correct |
25 |
Correct |
5 ms |
16732 KB |
Output is correct |
26 |
Correct |
4 ms |
16732 KB |
Output is correct |
27 |
Correct |
4 ms |
16476 KB |
Output is correct |
28 |
Correct |
4 ms |
16732 KB |
Output is correct |
29 |
Correct |
4 ms |
16988 KB |
Output is correct |
30 |
Correct |
5 ms |
16988 KB |
Output is correct |
31 |
Correct |
5 ms |
16988 KB |
Output is correct |
32 |
Correct |
6 ms |
16216 KB |
Output is correct |
33 |
Correct |
175 ms |
31576 KB |
Output is correct |
34 |
Correct |
165 ms |
30544 KB |
Output is correct |
35 |
Correct |
164 ms |
29524 KB |
Output is correct |
36 |
Correct |
181 ms |
30804 KB |
Output is correct |
37 |
Correct |
178 ms |
32848 KB |
Output is correct |
38 |
Correct |
186 ms |
31832 KB |
Output is correct |
39 |
Correct |
168 ms |
53588 KB |
Output is correct |
40 |
Correct |
149 ms |
53584 KB |
Output is correct |
41 |
Correct |
130 ms |
56632 KB |
Output is correct |
42 |
Correct |
114 ms |
37968 KB |
Output is correct |
43 |
Correct |
122 ms |
38204 KB |
Output is correct |
44 |
Correct |
157 ms |
31548 KB |
Output is correct |
45 |
Correct |
159 ms |
31568 KB |
Output is correct |
46 |
Correct |
217 ms |
35924 KB |
Output is correct |
47 |
Correct |
163 ms |
41800 KB |
Output is correct |
48 |
Correct |
164 ms |
45392 KB |
Output is correct |
49 |
Correct |
139 ms |
46160 KB |
Output is correct |
50 |
Correct |
99 ms |
29320 KB |
Output is correct |
51 |
Correct |
95 ms |
29416 KB |
Output is correct |
52 |
Correct |
142 ms |
42440 KB |
Output is correct |
53 |
Correct |
164 ms |
46284 KB |
Output is correct |
54 |
Correct |
61 ms |
53588 KB |
Output is correct |
55 |
Correct |
103 ms |
44116 KB |
Output is correct |
56 |
Correct |
110 ms |
54100 KB |
Output is correct |
57 |
Correct |
112 ms |
58688 KB |
Output is correct |
58 |
Correct |
140 ms |
56656 KB |
Output is correct |
59 |
Correct |
137 ms |
56576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
15708 KB |
Output is correct |
2 |
Correct |
2 ms |
15708 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
2 ms |
15708 KB |
Output is correct |
5 |
Correct |
5 ms |
16328 KB |
Output is correct |
6 |
Correct |
5 ms |
16220 KB |
Output is correct |
7 |
Correct |
5 ms |
16220 KB |
Output is correct |
8 |
Correct |
5 ms |
16220 KB |
Output is correct |
9 |
Correct |
5 ms |
16188 KB |
Output is correct |
10 |
Correct |
6 ms |
16220 KB |
Output is correct |
11 |
Correct |
6 ms |
16268 KB |
Output is correct |
12 |
Correct |
5 ms |
16732 KB |
Output is correct |
13 |
Correct |
6 ms |
16988 KB |
Output is correct |
14 |
Correct |
6 ms |
16476 KB |
Output is correct |
15 |
Correct |
7 ms |
16476 KB |
Output is correct |
16 |
Correct |
6 ms |
16220 KB |
Output is correct |
17 |
Correct |
6 ms |
16220 KB |
Output is correct |
18 |
Correct |
7 ms |
16476 KB |
Output is correct |
19 |
Correct |
5 ms |
16632 KB |
Output is correct |
20 |
Correct |
5 ms |
16472 KB |
Output is correct |
21 |
Correct |
4 ms |
16732 KB |
Output is correct |
22 |
Correct |
5 ms |
16220 KB |
Output is correct |
23 |
Correct |
4 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16476 KB |
Output is correct |
25 |
Correct |
5 ms |
16732 KB |
Output is correct |
26 |
Correct |
4 ms |
16732 KB |
Output is correct |
27 |
Correct |
4 ms |
16476 KB |
Output is correct |
28 |
Correct |
4 ms |
16732 KB |
Output is correct |
29 |
Correct |
4 ms |
16988 KB |
Output is correct |
30 |
Correct |
5 ms |
16988 KB |
Output is correct |
31 |
Correct |
5 ms |
16988 KB |
Output is correct |
32 |
Correct |
6 ms |
16216 KB |
Output is correct |
33 |
Correct |
175 ms |
31576 KB |
Output is correct |
34 |
Correct |
165 ms |
30544 KB |
Output is correct |
35 |
Correct |
164 ms |
29524 KB |
Output is correct |
36 |
Correct |
181 ms |
30804 KB |
Output is correct |
37 |
Correct |
178 ms |
32848 KB |
Output is correct |
38 |
Correct |
186 ms |
31832 KB |
Output is correct |
39 |
Correct |
168 ms |
53588 KB |
Output is correct |
40 |
Correct |
149 ms |
53584 KB |
Output is correct |
41 |
Correct |
130 ms |
56632 KB |
Output is correct |
42 |
Correct |
114 ms |
37968 KB |
Output is correct |
43 |
Correct |
122 ms |
38204 KB |
Output is correct |
44 |
Correct |
157 ms |
31548 KB |
Output is correct |
45 |
Correct |
159 ms |
31568 KB |
Output is correct |
46 |
Correct |
217 ms |
35924 KB |
Output is correct |
47 |
Correct |
163 ms |
41800 KB |
Output is correct |
48 |
Correct |
164 ms |
45392 KB |
Output is correct |
49 |
Correct |
139 ms |
46160 KB |
Output is correct |
50 |
Correct |
99 ms |
29320 KB |
Output is correct |
51 |
Correct |
95 ms |
29416 KB |
Output is correct |
52 |
Correct |
142 ms |
42440 KB |
Output is correct |
53 |
Correct |
164 ms |
46284 KB |
Output is correct |
54 |
Correct |
61 ms |
53588 KB |
Output is correct |
55 |
Correct |
103 ms |
44116 KB |
Output is correct |
56 |
Correct |
110 ms |
54100 KB |
Output is correct |
57 |
Correct |
112 ms |
58688 KB |
Output is correct |
58 |
Correct |
140 ms |
56656 KB |
Output is correct |
59 |
Correct |
137 ms |
56576 KB |
Output is correct |
60 |
Correct |
2 ms |
15708 KB |
Output is correct |
61 |
Correct |
2 ms |
15708 KB |
Output is correct |
62 |
Correct |
2 ms |
15708 KB |
Output is correct |
63 |
Correct |
2 ms |
15944 KB |
Output is correct |
64 |
Correct |
166 ms |
28476 KB |
Output is correct |
65 |
Correct |
148 ms |
26944 KB |
Output is correct |
66 |
Correct |
152 ms |
31056 KB |
Output is correct |
67 |
Correct |
174 ms |
31824 KB |
Output is correct |
68 |
Correct |
186 ms |
32848 KB |
Output is correct |
69 |
Correct |
169 ms |
26404 KB |
Output is correct |
70 |
Correct |
144 ms |
46588 KB |
Output is correct |
71 |
Correct |
168 ms |
41048 KB |
Output is correct |
72 |
Correct |
158 ms |
56380 KB |
Output is correct |
73 |
Correct |
175 ms |
56440 KB |
Output is correct |
74 |
Correct |
150 ms |
45720 KB |
Output is correct |
75 |
Correct |
157 ms |
48828 KB |
Output is correct |
76 |
Correct |
141 ms |
49052 KB |
Output is correct |
77 |
Correct |
171 ms |
45720 KB |
Output is correct |
78 |
Correct |
148 ms |
48880 KB |
Output is correct |
79 |
Correct |
167 ms |
45464 KB |
Output is correct |
80 |
Correct |
188 ms |
48284 KB |
Output is correct |
81 |
Correct |
165 ms |
49180 KB |
Output is correct |
82 |
Correct |
127 ms |
56352 KB |
Output is correct |
83 |
Correct |
52 ms |
18904 KB |
Output is correct |
84 |
Correct |
173 ms |
53076 KB |
Output is correct |
85 |
Correct |
148 ms |
52888 KB |
Output is correct |
86 |
Correct |
160 ms |
52924 KB |
Output is correct |
87 |
Correct |
166 ms |
52876 KB |
Output is correct |
88 |
Correct |
149 ms |
52856 KB |
Output is correct |