#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), h(n), c(n);
for (int u = 0; u < n; u++) {
cin >> a[u] >> h[u] >> c[u];
a[u]--;
}
vector<map<int, long long>> f(n);
auto add = [](map<int, long long> &x, map<int, long long> &y) {
if (y.size() > x.size()) swap(x, y);
for (auto [h, c] : y) x[h] += c;
};
auto fix = [](map<int, long long> &x, int h, long long c) {
x[h] += c;
auto it = x.lower_bound(h);
while (it != x.begin()) {
it = prev(it);
if (it->second <= c) {
c -= it->second;
it = x.erase(it);
} else {
it->second -= c;
break;
}
}
};
vector<int> deg(n, 0);
queue<int> que;
for (int u = 0; u < n; u++) deg[a[u]]++;
for (int u = 0; u < n; u++) if (!deg[u]) que.push(u);
while (!que.empty()) {
int u = que.front();
que.pop();
fix(f[u], h[u], c[u]);
add(f[a[u]], f[u]);
if (!--deg[a[u]]) que.push(a[u]);
}
long long res = 0;
for (int u = 0; u < n; u++) {
if (deg[u] > 0) {
map<int, long long> sum;
sum[h[u]] += c[u];
deg[u] = 0;
for (int v = a[u]; v != u; v = a[v]) {
add(f[u], f[v]);
sum[h[v]] += c[v];
deg[v] = 0;
}
for (auto [h, c] : sum) {
fix(f[u], h, c);
}
for (auto [h, c] : f[u]) {
res += c;
}
}
}
cout << accumulate(c.begin(), c.end(), 0LL) - res << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
3 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
5 ms |
2136 KB |
Output is correct |
9 |
Correct |
5 ms |
1368 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
756 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
3 ms |
884 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
5 ms |
2140 KB |
Output is correct |
17 |
Correct |
3 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
984 KB |
Output is correct |
22 |
Correct |
3 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
4 ms |
1648 KB |
Output is correct |
28 |
Correct |
3 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1116 KB |
Output is correct |
30 |
Correct |
3 ms |
1116 KB |
Output is correct |
31 |
Correct |
3 ms |
1136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
3 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
5 ms |
2136 KB |
Output is correct |
9 |
Correct |
5 ms |
1368 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
756 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
3 ms |
884 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
5 ms |
2140 KB |
Output is correct |
17 |
Correct |
3 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
984 KB |
Output is correct |
22 |
Correct |
3 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
4 ms |
1648 KB |
Output is correct |
28 |
Correct |
3 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1116 KB |
Output is correct |
30 |
Correct |
3 ms |
1116 KB |
Output is correct |
31 |
Correct |
3 ms |
1136 KB |
Output is correct |
32 |
Correct |
6 ms |
1904 KB |
Output is correct |
33 |
Correct |
291 ms |
86612 KB |
Output is correct |
34 |
Correct |
225 ms |
56404 KB |
Output is correct |
35 |
Correct |
278 ms |
86120 KB |
Output is correct |
36 |
Correct |
211 ms |
55892 KB |
Output is correct |
37 |
Correct |
96 ms |
29776 KB |
Output is correct |
38 |
Correct |
68 ms |
24156 KB |
Output is correct |
39 |
Correct |
85 ms |
18168 KB |
Output is correct |
40 |
Correct |
77 ms |
18132 KB |
Output is correct |
41 |
Correct |
60 ms |
17904 KB |
Output is correct |
42 |
Correct |
85 ms |
18260 KB |
Output is correct |
43 |
Correct |
78 ms |
18004 KB |
Output is correct |
44 |
Correct |
272 ms |
97360 KB |
Output is correct |
45 |
Correct |
171 ms |
61664 KB |
Output is correct |
46 |
Correct |
56 ms |
24400 KB |
Output is correct |
47 |
Correct |
127 ms |
31212 KB |
Output is correct |
48 |
Correct |
72 ms |
24144 KB |
Output is correct |
49 |
Correct |
69 ms |
24184 KB |
Output is correct |
50 |
Correct |
189 ms |
42080 KB |
Output is correct |
51 |
Correct |
69 ms |
29784 KB |
Output is correct |
52 |
Correct |
174 ms |
31344 KB |
Output is correct |
53 |
Correct |
72 ms |
24404 KB |
Output is correct |
54 |
Correct |
57 ms |
18156 KB |
Output is correct |
55 |
Correct |
106 ms |
35376 KB |
Output is correct |
56 |
Correct |
86 ms |
30216 KB |
Output is correct |
57 |
Correct |
85 ms |
29684 KB |
Output is correct |
58 |
Correct |
134 ms |
36180 KB |
Output is correct |
59 |
Correct |
158 ms |
35952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
3 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
5 ms |
2136 KB |
Output is correct |
9 |
Correct |
5 ms |
1368 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
756 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
3 ms |
884 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
5 ms |
2140 KB |
Output is correct |
17 |
Correct |
3 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
984 KB |
Output is correct |
22 |
Correct |
3 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
4 ms |
1648 KB |
Output is correct |
28 |
Correct |
3 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1116 KB |
Output is correct |
30 |
Correct |
3 ms |
1116 KB |
Output is correct |
31 |
Correct |
3 ms |
1136 KB |
Output is correct |
32 |
Correct |
6 ms |
1904 KB |
Output is correct |
33 |
Correct |
291 ms |
86612 KB |
Output is correct |
34 |
Correct |
225 ms |
56404 KB |
Output is correct |
35 |
Correct |
278 ms |
86120 KB |
Output is correct |
36 |
Correct |
211 ms |
55892 KB |
Output is correct |
37 |
Correct |
96 ms |
29776 KB |
Output is correct |
38 |
Correct |
68 ms |
24156 KB |
Output is correct |
39 |
Correct |
85 ms |
18168 KB |
Output is correct |
40 |
Correct |
77 ms |
18132 KB |
Output is correct |
41 |
Correct |
60 ms |
17904 KB |
Output is correct |
42 |
Correct |
85 ms |
18260 KB |
Output is correct |
43 |
Correct |
78 ms |
18004 KB |
Output is correct |
44 |
Correct |
272 ms |
97360 KB |
Output is correct |
45 |
Correct |
171 ms |
61664 KB |
Output is correct |
46 |
Correct |
56 ms |
24400 KB |
Output is correct |
47 |
Correct |
127 ms |
31212 KB |
Output is correct |
48 |
Correct |
72 ms |
24144 KB |
Output is correct |
49 |
Correct |
69 ms |
24184 KB |
Output is correct |
50 |
Correct |
189 ms |
42080 KB |
Output is correct |
51 |
Correct |
69 ms |
29784 KB |
Output is correct |
52 |
Correct |
174 ms |
31344 KB |
Output is correct |
53 |
Correct |
72 ms |
24404 KB |
Output is correct |
54 |
Correct |
57 ms |
18156 KB |
Output is correct |
55 |
Correct |
106 ms |
35376 KB |
Output is correct |
56 |
Correct |
86 ms |
30216 KB |
Output is correct |
57 |
Correct |
85 ms |
29684 KB |
Output is correct |
58 |
Correct |
134 ms |
36180 KB |
Output is correct |
59 |
Correct |
158 ms |
35952 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
218 ms |
57784 KB |
Output is correct |
65 |
Correct |
175 ms |
41300 KB |
Output is correct |
66 |
Correct |
231 ms |
57428 KB |
Output is correct |
67 |
Correct |
177 ms |
41044 KB |
Output is correct |
68 |
Correct |
93 ms |
26452 KB |
Output is correct |
69 |
Correct |
70 ms |
22864 KB |
Output is correct |
70 |
Correct |
139 ms |
26528 KB |
Output is correct |
71 |
Correct |
64 ms |
18000 KB |
Output is correct |
72 |
Correct |
189 ms |
30624 KB |
Output is correct |
73 |
Correct |
66 ms |
18000 KB |
Output is correct |
74 |
Correct |
204 ms |
38116 KB |
Output is correct |
75 |
Correct |
89 ms |
24412 KB |
Output is correct |
76 |
Correct |
76 ms |
24400 KB |
Output is correct |
77 |
Correct |
198 ms |
38396 KB |
Output is correct |
78 |
Correct |
75 ms |
24268 KB |
Output is correct |
79 |
Correct |
227 ms |
57100 KB |
Output is correct |
80 |
Correct |
138 ms |
37328 KB |
Output is correct |
81 |
Correct |
78 ms |
23892 KB |
Output is correct |
82 |
Correct |
131 ms |
30804 KB |
Output is correct |
83 |
Correct |
66 ms |
30544 KB |
Output is correct |
84 |
Correct |
147 ms |
33560 KB |
Output is correct |
85 |
Correct |
135 ms |
33260 KB |
Output is correct |
86 |
Correct |
140 ms |
30056 KB |
Output is correct |
87 |
Correct |
143 ms |
33164 KB |
Output is correct |
88 |
Correct |
145 ms |
33116 KB |
Output is correct |