#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 |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
6 ms |
1792 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
964 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
2140 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
856 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 |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
3 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
1112 KB |
Output is correct |
28 |
Correct |
3 ms |
1116 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
3 ms |
1116 KB |
Output is correct |
31 |
Correct |
3 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
6 ms |
1792 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
964 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
2140 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
856 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 |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
3 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
1112 KB |
Output is correct |
28 |
Correct |
3 ms |
1116 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
3 ms |
1116 KB |
Output is correct |
31 |
Correct |
3 ms |
1368 KB |
Output is correct |
32 |
Correct |
5 ms |
1884 KB |
Output is correct |
33 |
Correct |
284 ms |
81760 KB |
Output is correct |
34 |
Correct |
209 ms |
51284 KB |
Output is correct |
35 |
Correct |
289 ms |
81016 KB |
Output is correct |
36 |
Correct |
226 ms |
51024 KB |
Output is correct |
37 |
Correct |
99 ms |
24656 KB |
Output is correct |
38 |
Correct |
75 ms |
19208 KB |
Output is correct |
39 |
Correct |
83 ms |
12880 KB |
Output is correct |
40 |
Correct |
81 ms |
12880 KB |
Output is correct |
41 |
Correct |
51 ms |
12884 KB |
Output is correct |
42 |
Correct |
85 ms |
13136 KB |
Output is correct |
43 |
Correct |
79 ms |
12880 KB |
Output is correct |
44 |
Correct |
273 ms |
92400 KB |
Output is correct |
45 |
Correct |
181 ms |
56680 KB |
Output is correct |
46 |
Correct |
67 ms |
19700 KB |
Output is correct |
47 |
Correct |
126 ms |
26192 KB |
Output is correct |
48 |
Correct |
70 ms |
19284 KB |
Output is correct |
49 |
Correct |
56 ms |
19028 KB |
Output is correct |
50 |
Correct |
199 ms |
37980 KB |
Output is correct |
51 |
Correct |
68 ms |
25600 KB |
Output is correct |
52 |
Correct |
148 ms |
26344 KB |
Output is correct |
53 |
Correct |
82 ms |
19184 KB |
Output is correct |
54 |
Correct |
55 ms |
12808 KB |
Output is correct |
55 |
Correct |
114 ms |
31056 KB |
Output is correct |
56 |
Correct |
87 ms |
26144 KB |
Output is correct |
57 |
Correct |
79 ms |
25344 KB |
Output is correct |
58 |
Correct |
140 ms |
31748 KB |
Output is correct |
59 |
Correct |
133 ms |
31572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
6 ms |
1792 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
964 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
2140 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
856 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 |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
1368 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
3 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
1112 KB |
Output is correct |
28 |
Correct |
3 ms |
1116 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
3 ms |
1116 KB |
Output is correct |
31 |
Correct |
3 ms |
1368 KB |
Output is correct |
32 |
Correct |
5 ms |
1884 KB |
Output is correct |
33 |
Correct |
284 ms |
81760 KB |
Output is correct |
34 |
Correct |
209 ms |
51284 KB |
Output is correct |
35 |
Correct |
289 ms |
81016 KB |
Output is correct |
36 |
Correct |
226 ms |
51024 KB |
Output is correct |
37 |
Correct |
99 ms |
24656 KB |
Output is correct |
38 |
Correct |
75 ms |
19208 KB |
Output is correct |
39 |
Correct |
83 ms |
12880 KB |
Output is correct |
40 |
Correct |
81 ms |
12880 KB |
Output is correct |
41 |
Correct |
51 ms |
12884 KB |
Output is correct |
42 |
Correct |
85 ms |
13136 KB |
Output is correct |
43 |
Correct |
79 ms |
12880 KB |
Output is correct |
44 |
Correct |
273 ms |
92400 KB |
Output is correct |
45 |
Correct |
181 ms |
56680 KB |
Output is correct |
46 |
Correct |
67 ms |
19700 KB |
Output is correct |
47 |
Correct |
126 ms |
26192 KB |
Output is correct |
48 |
Correct |
70 ms |
19284 KB |
Output is correct |
49 |
Correct |
56 ms |
19028 KB |
Output is correct |
50 |
Correct |
199 ms |
37980 KB |
Output is correct |
51 |
Correct |
68 ms |
25600 KB |
Output is correct |
52 |
Correct |
148 ms |
26344 KB |
Output is correct |
53 |
Correct |
82 ms |
19184 KB |
Output is correct |
54 |
Correct |
55 ms |
12808 KB |
Output is correct |
55 |
Correct |
114 ms |
31056 KB |
Output is correct |
56 |
Correct |
87 ms |
26144 KB |
Output is correct |
57 |
Correct |
79 ms |
25344 KB |
Output is correct |
58 |
Correct |
140 ms |
31748 KB |
Output is correct |
59 |
Correct |
133 ms |
31572 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
0 ms |
344 KB |
Output is correct |
62 |
Correct |
1 ms |
456 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
259 ms |
58000 KB |
Output is correct |
65 |
Correct |
172 ms |
41292 KB |
Output is correct |
66 |
Correct |
226 ms |
57432 KB |
Output is correct |
67 |
Correct |
172 ms |
41044 KB |
Output is correct |
68 |
Correct |
90 ms |
26476 KB |
Output is correct |
69 |
Correct |
71 ms |
22772 KB |
Output is correct |
70 |
Correct |
146 ms |
26688 KB |
Output is correct |
71 |
Correct |
65 ms |
18024 KB |
Output is correct |
72 |
Correct |
164 ms |
30392 KB |
Output is correct |
73 |
Correct |
66 ms |
18004 KB |
Output is correct |
74 |
Correct |
211 ms |
38228 KB |
Output is correct |
75 |
Correct |
103 ms |
24528 KB |
Output is correct |
76 |
Correct |
75 ms |
24404 KB |
Output is correct |
77 |
Correct |
175 ms |
38244 KB |
Output is correct |
78 |
Correct |
71 ms |
24400 KB |
Output is correct |
79 |
Correct |
215 ms |
57452 KB |
Output is correct |
80 |
Correct |
136 ms |
37112 KB |
Output is correct |
81 |
Correct |
75 ms |
24048 KB |
Output is correct |
82 |
Correct |
111 ms |
30656 KB |
Output is correct |
83 |
Correct |
66 ms |
30568 KB |
Output is correct |
84 |
Correct |
142 ms |
33104 KB |
Output is correct |
85 |
Correct |
147 ms |
33144 KB |
Output is correct |
86 |
Correct |
137 ms |
30088 KB |
Output is correct |
87 |
Correct |
149 ms |
33084 KB |
Output is correct |
88 |
Correct |
139 ms |
33104 KB |
Output is correct |