#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, idx, totC, totR, A[200005], H[200005], C[200005], link[200005], sz[200005];
bool on_cyc[200005];
vector<int> adj[200005];
set<iii> S[200005];
int find(int x) {
if (x == link[x]) return x;
return link[x] = find(link[x]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
if (sz[b] > sz[a]) swap(a, b);
sz[a] += sz[b];
link[b] = a;
}
void dp(int n, int e = -1) {
for (auto u : adj[n]) if (u != e) {
dp(u, n);
if (S[u].size() > S[n].size()) swap(S[n], S[u]);
for (auto i : S[u]) S[n].insert(i);
}
vector<set<iii>::iterator> to_del;
auto it = S[n].upper_bound(mt(H[n], LLONG_MAX, LLONG_MAX));
for (int bal = C[n]; bal > 0 && it != S[n].end(); it++) {
auto [h, c, tmp] = *it;
if (bal > c) {
to_del.pb(it);
bal -= c;
} else if (bal == c) {
to_del.pb(it);
bal = 0;
} else {
to_del.pb(it);
S[n].insert(mt(h, c - bal, ++idx));
bal = 0;
}
}
S[n].insert(mt(H[n], C[n], ++idx));
for (auto i : to_del) S[n].erase(i);
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
link[i] = i;
sz[i] = 1;
}
for (int i = 1; i <= N; i++) {
cin >> A[i] >> H[i] >> C[i];
H[i] = -H[i];
adj[A[i]].pb(i);
unite(i, A[i]);
totC += C[i];
}
for (int i = 1; i <= N; i++)
if (i == find(i)) {
set<iii> SS;
map<int, int> mpp;
int tort = i, hare = i;
do {
tort = A[tort];
hare = A[A[hare]];
} while (tort != hare);
do {
on_cyc[tort] = 1;
tort = A[tort];
} while (tort != hare);
do {
for (auto u : adj[tort])
if (!on_cyc[u]) {
dp(u);
if (SS.size() < S[u].size()) swap(SS, S[u]);
for (auto v : S[u]) SS.insert(v);
}
tort = A[tort];
mpp[H[tort]] += C[tort];
} while (tort != hare);
auto it = SS.begin();
int sf = 0, cans = 0;
for (auto [a, b, c] : SS) cans += b;
for (auto j : mpp) {
while (it != SS.end() && g0(*it) <= j.first) sf += g1(*it), ++it;
cans = max(cans, sf + j.second);
}
totR += cans;
}
cout << totC - totR << '\n';
}
Compilation message
worst_reporter2.cpp:74:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
74 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
12 ms |
15700 KB |
Output is correct |
6 |
Correct |
14 ms |
15624 KB |
Output is correct |
7 |
Correct |
13 ms |
15600 KB |
Output is correct |
8 |
Correct |
13 ms |
15700 KB |
Output is correct |
9 |
Correct |
16 ms |
15664 KB |
Output is correct |
10 |
Correct |
13 ms |
15700 KB |
Output is correct |
11 |
Correct |
16 ms |
15952 KB |
Output is correct |
12 |
Correct |
11 ms |
15588 KB |
Output is correct |
13 |
Correct |
11 ms |
15600 KB |
Output is correct |
14 |
Correct |
11 ms |
15256 KB |
Output is correct |
15 |
Correct |
11 ms |
15256 KB |
Output is correct |
16 |
Correct |
12 ms |
16040 KB |
Output is correct |
17 |
Correct |
12 ms |
16008 KB |
Output is correct |
18 |
Correct |
13 ms |
16596 KB |
Output is correct |
19 |
Correct |
12 ms |
15500 KB |
Output is correct |
20 |
Correct |
11 ms |
15316 KB |
Output is correct |
21 |
Correct |
11 ms |
15324 KB |
Output is correct |
22 |
Correct |
11 ms |
15316 KB |
Output is correct |
23 |
Correct |
12 ms |
15316 KB |
Output is correct |
24 |
Correct |
13 ms |
15520 KB |
Output is correct |
25 |
Correct |
14 ms |
15444 KB |
Output is correct |
26 |
Correct |
13 ms |
15572 KB |
Output is correct |
27 |
Correct |
12 ms |
15444 KB |
Output is correct |
28 |
Correct |
11 ms |
15524 KB |
Output is correct |
29 |
Correct |
11 ms |
15700 KB |
Output is correct |
30 |
Correct |
11 ms |
15700 KB |
Output is correct |
31 |
Correct |
11 ms |
15608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
12 ms |
15700 KB |
Output is correct |
6 |
Correct |
14 ms |
15624 KB |
Output is correct |
7 |
Correct |
13 ms |
15600 KB |
Output is correct |
8 |
Correct |
13 ms |
15700 KB |
Output is correct |
9 |
Correct |
16 ms |
15664 KB |
Output is correct |
10 |
Correct |
13 ms |
15700 KB |
Output is correct |
11 |
Correct |
16 ms |
15952 KB |
Output is correct |
12 |
Correct |
11 ms |
15588 KB |
Output is correct |
13 |
Correct |
11 ms |
15600 KB |
Output is correct |
14 |
Correct |
11 ms |
15256 KB |
Output is correct |
15 |
Correct |
11 ms |
15256 KB |
Output is correct |
16 |
Correct |
12 ms |
16040 KB |
Output is correct |
17 |
Correct |
12 ms |
16008 KB |
Output is correct |
18 |
Correct |
13 ms |
16596 KB |
Output is correct |
19 |
Correct |
12 ms |
15500 KB |
Output is correct |
20 |
Correct |
11 ms |
15316 KB |
Output is correct |
21 |
Correct |
11 ms |
15324 KB |
Output is correct |
22 |
Correct |
11 ms |
15316 KB |
Output is correct |
23 |
Correct |
12 ms |
15316 KB |
Output is correct |
24 |
Correct |
13 ms |
15520 KB |
Output is correct |
25 |
Correct |
14 ms |
15444 KB |
Output is correct |
26 |
Correct |
13 ms |
15572 KB |
Output is correct |
27 |
Correct |
12 ms |
15444 KB |
Output is correct |
28 |
Correct |
11 ms |
15524 KB |
Output is correct |
29 |
Correct |
11 ms |
15700 KB |
Output is correct |
30 |
Correct |
11 ms |
15700 KB |
Output is correct |
31 |
Correct |
11 ms |
15608 KB |
Output is correct |
32 |
Correct |
12 ms |
15700 KB |
Output is correct |
33 |
Correct |
326 ms |
80392 KB |
Output is correct |
34 |
Correct |
349 ms |
79856 KB |
Output is correct |
35 |
Correct |
316 ms |
77532 KB |
Output is correct |
36 |
Correct |
331 ms |
79988 KB |
Output is correct |
37 |
Correct |
336 ms |
77760 KB |
Output is correct |
38 |
Correct |
369 ms |
92012 KB |
Output is correct |
39 |
Correct |
185 ms |
63012 KB |
Output is correct |
40 |
Correct |
187 ms |
63052 KB |
Output is correct |
41 |
Correct |
175 ms |
65416 KB |
Output is correct |
42 |
Correct |
173 ms |
45812 KB |
Output is correct |
43 |
Correct |
177 ms |
45800 KB |
Output is correct |
44 |
Correct |
314 ms |
104880 KB |
Output is correct |
45 |
Correct |
323 ms |
104908 KB |
Output is correct |
46 |
Correct |
400 ms |
133620 KB |
Output is correct |
47 |
Correct |
252 ms |
55788 KB |
Output is correct |
48 |
Correct |
247 ms |
52700 KB |
Output is correct |
49 |
Correct |
242 ms |
54272 KB |
Output is correct |
50 |
Correct |
262 ms |
48832 KB |
Output is correct |
51 |
Correct |
273 ms |
48756 KB |
Output is correct |
52 |
Correct |
323 ms |
56700 KB |
Output is correct |
53 |
Correct |
263 ms |
55860 KB |
Output is correct |
54 |
Correct |
115 ms |
62932 KB |
Output is correct |
55 |
Correct |
227 ms |
58848 KB |
Output is correct |
56 |
Correct |
218 ms |
65912 KB |
Output is correct |
57 |
Correct |
203 ms |
69440 KB |
Output is correct |
58 |
Correct |
205 ms |
64372 KB |
Output is correct |
59 |
Correct |
195 ms |
64416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
12 ms |
15700 KB |
Output is correct |
6 |
Correct |
14 ms |
15624 KB |
Output is correct |
7 |
Correct |
13 ms |
15600 KB |
Output is correct |
8 |
Correct |
13 ms |
15700 KB |
Output is correct |
9 |
Correct |
16 ms |
15664 KB |
Output is correct |
10 |
Correct |
13 ms |
15700 KB |
Output is correct |
11 |
Correct |
16 ms |
15952 KB |
Output is correct |
12 |
Correct |
11 ms |
15588 KB |
Output is correct |
13 |
Correct |
11 ms |
15600 KB |
Output is correct |
14 |
Correct |
11 ms |
15256 KB |
Output is correct |
15 |
Correct |
11 ms |
15256 KB |
Output is correct |
16 |
Correct |
12 ms |
16040 KB |
Output is correct |
17 |
Correct |
12 ms |
16008 KB |
Output is correct |
18 |
Correct |
13 ms |
16596 KB |
Output is correct |
19 |
Correct |
12 ms |
15500 KB |
Output is correct |
20 |
Correct |
11 ms |
15316 KB |
Output is correct |
21 |
Correct |
11 ms |
15324 KB |
Output is correct |
22 |
Correct |
11 ms |
15316 KB |
Output is correct |
23 |
Correct |
12 ms |
15316 KB |
Output is correct |
24 |
Correct |
13 ms |
15520 KB |
Output is correct |
25 |
Correct |
14 ms |
15444 KB |
Output is correct |
26 |
Correct |
13 ms |
15572 KB |
Output is correct |
27 |
Correct |
12 ms |
15444 KB |
Output is correct |
28 |
Correct |
11 ms |
15524 KB |
Output is correct |
29 |
Correct |
11 ms |
15700 KB |
Output is correct |
30 |
Correct |
11 ms |
15700 KB |
Output is correct |
31 |
Correct |
11 ms |
15608 KB |
Output is correct |
32 |
Correct |
12 ms |
15700 KB |
Output is correct |
33 |
Correct |
326 ms |
80392 KB |
Output is correct |
34 |
Correct |
349 ms |
79856 KB |
Output is correct |
35 |
Correct |
316 ms |
77532 KB |
Output is correct |
36 |
Correct |
331 ms |
79988 KB |
Output is correct |
37 |
Correct |
336 ms |
77760 KB |
Output is correct |
38 |
Correct |
369 ms |
92012 KB |
Output is correct |
39 |
Correct |
185 ms |
63012 KB |
Output is correct |
40 |
Correct |
187 ms |
63052 KB |
Output is correct |
41 |
Correct |
175 ms |
65416 KB |
Output is correct |
42 |
Correct |
173 ms |
45812 KB |
Output is correct |
43 |
Correct |
177 ms |
45800 KB |
Output is correct |
44 |
Correct |
314 ms |
104880 KB |
Output is correct |
45 |
Correct |
323 ms |
104908 KB |
Output is correct |
46 |
Correct |
400 ms |
133620 KB |
Output is correct |
47 |
Correct |
252 ms |
55788 KB |
Output is correct |
48 |
Correct |
247 ms |
52700 KB |
Output is correct |
49 |
Correct |
242 ms |
54272 KB |
Output is correct |
50 |
Correct |
262 ms |
48832 KB |
Output is correct |
51 |
Correct |
273 ms |
48756 KB |
Output is correct |
52 |
Correct |
323 ms |
56700 KB |
Output is correct |
53 |
Correct |
263 ms |
55860 KB |
Output is correct |
54 |
Correct |
115 ms |
62932 KB |
Output is correct |
55 |
Correct |
227 ms |
58848 KB |
Output is correct |
56 |
Correct |
218 ms |
65912 KB |
Output is correct |
57 |
Correct |
203 ms |
69440 KB |
Output is correct |
58 |
Correct |
205 ms |
64372 KB |
Output is correct |
59 |
Correct |
195 ms |
64416 KB |
Output is correct |
60 |
Correct |
8 ms |
14420 KB |
Output is correct |
61 |
Correct |
7 ms |
14420 KB |
Output is correct |
62 |
Correct |
8 ms |
14420 KB |
Output is correct |
63 |
Correct |
7 ms |
14364 KB |
Output is correct |
64 |
Correct |
373 ms |
70128 KB |
Output is correct |
65 |
Correct |
341 ms |
62384 KB |
Output is correct |
66 |
Correct |
364 ms |
65448 KB |
Output is correct |
67 |
Correct |
345 ms |
68936 KB |
Output is correct |
68 |
Correct |
369 ms |
62244 KB |
Output is correct |
69 |
Correct |
337 ms |
72608 KB |
Output is correct |
70 |
Correct |
319 ms |
42432 KB |
Output is correct |
71 |
Correct |
176 ms |
33824 KB |
Output is correct |
72 |
Correct |
295 ms |
46344 KB |
Output is correct |
73 |
Correct |
193 ms |
33884 KB |
Output is correct |
74 |
Correct |
338 ms |
49476 KB |
Output is correct |
75 |
Correct |
320 ms |
43300 KB |
Output is correct |
76 |
Correct |
248 ms |
43216 KB |
Output is correct |
77 |
Correct |
234 ms |
50248 KB |
Output is correct |
78 |
Correct |
240 ms |
43912 KB |
Output is correct |
79 |
Correct |
319 ms |
65744 KB |
Output is correct |
80 |
Correct |
275 ms |
59920 KB |
Output is correct |
81 |
Correct |
271 ms |
59288 KB |
Output is correct |
82 |
Correct |
202 ms |
46540 KB |
Output is correct |
83 |
Correct |
89 ms |
33800 KB |
Output is correct |
84 |
Correct |
308 ms |
55992 KB |
Output is correct |
85 |
Correct |
285 ms |
55964 KB |
Output is correct |
86 |
Correct |
307 ms |
52812 KB |
Output is correct |
87 |
Correct |
273 ms |
55936 KB |
Output is correct |
88 |
Correct |
320 ms |
55900 KB |
Output is correct |