#include <bits/stdc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) qqbx(#a, a)
#define pary(a...) danb(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
std::cerr << "\033[1;32m[ " << s << " ] = [ ";
for (auto it = L; it != R; ++it)
std::cerr << *it << ' ';
std::cerr << "]\033[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
using namespace std;
using ll = int64_t;
const int maxn = 200025, maxm = 10000;
int H[maxn], C[maxn];
vector<int> g[maxn];
map<int,ll,greater<>> sub[maxn];
int cyc[maxn];
void join(int a, int b) {
if (sub[a].size() > sub[b].size())
swap(sub[a], sub[b]);
for (auto [h, val]: sub[a])
sub[b][h] += val;
}
void dfs(int i) {
for (int j: g[i]) {
dfs(j);
join(j, i);
}
sub[i][H[i]] += C[i];
auto it = sub[i].upper_bound(H[i]);
ll v = C[i];
while (it != sub[i].end()) {
if (it->second < v) {
v -= it->second;
sub[i].erase(it++);
} else {
it->second -= v;
break;
}
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
ll tot = 0;
vector<int> A(n);
vector<int> indeg(n);
for (int i = 0; i < n; i++) {
cin >> A[i] >> H[i] >> C[i];
tot += C[i];
--A[i];
++indeg[A[i]];
}
for (int i = 0; i < n; i++) cyc[i] = 1;
queue<int> que;
for (int i = 0; i < n; i++) if (indeg[i] == 0) que.push(i), cyc[i] = 0;
while (!que.empty()) {
int i = que.front(); que.pop();
int j = A[i];
g[j].push_back(i);
if (--indeg[j] == 0)
que.push(j), cyc[j] = 0;
}
ll ans = 0;
for (int i = 0, tot = 0; i < n; i++) if (cyc[i] == 1) {
vector<int> cycle;
cycle.emplace_back(i);
cyc[i] = -1;
for (int x = A[i]; x != i; x = A[x]) {
cycle.emplace_back(x);
cyc[x] = -1;
}
sort(all(cycle), [&](int a, int b){ return H[a] < H[b]; });
int last = -1;
for (int x: cycle) {
if (x != cycle[0]) {
for (int y: g[x]) {
g[cycle[0]].push_back(y);
}
g[x].clear();
g[x].push_back(last);
}
last = x;
}
dfs(cycle.back());
for (auto [h, val]: sub[cycle.back()]) ans += val;
}
cout << tot - ans << '\n';
}
Compilation message
worst_reporter2.cpp: In function 'void join(int, int)':
worst_reporter2.cpp:34:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for (auto [h, val]: sub[a])
| ^
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:100:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
100 | for (auto [h, val]: sub[cycle.back()]) ans += val;
| ^
worst_reporter2.cpp:79:21: warning: unused variable 'tot' [-Wunused-variable]
79 | for (int i = 0, tot = 0; i < n; i++) if (cyc[i] == 1) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14356 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15752 KB |
Output is correct |
6 |
Correct |
14 ms |
15180 KB |
Output is correct |
7 |
Correct |
16 ms |
14968 KB |
Output is correct |
8 |
Correct |
16 ms |
15820 KB |
Output is correct |
9 |
Correct |
14 ms |
15180 KB |
Output is correct |
10 |
Correct |
13 ms |
14860 KB |
Output is correct |
11 |
Correct |
12 ms |
14668 KB |
Output is correct |
12 |
Correct |
13 ms |
15072 KB |
Output is correct |
13 |
Correct |
12 ms |
15052 KB |
Output is correct |
14 |
Correct |
13 ms |
14796 KB |
Output is correct |
15 |
Correct |
12 ms |
14836 KB |
Output is correct |
16 |
Correct |
16 ms |
15820 KB |
Output is correct |
17 |
Correct |
14 ms |
15308 KB |
Output is correct |
18 |
Correct |
12 ms |
14640 KB |
Output is correct |
19 |
Correct |
14 ms |
15052 KB |
Output is correct |
20 |
Correct |
13 ms |
14952 KB |
Output is correct |
21 |
Correct |
13 ms |
14940 KB |
Output is correct |
22 |
Correct |
16 ms |
15148 KB |
Output is correct |
23 |
Correct |
12 ms |
14796 KB |
Output is correct |
24 |
Correct |
15 ms |
15108 KB |
Output is correct |
25 |
Correct |
14 ms |
14948 KB |
Output is correct |
26 |
Correct |
16 ms |
15356 KB |
Output is correct |
27 |
Correct |
14 ms |
15176 KB |
Output is correct |
28 |
Correct |
13 ms |
15180 KB |
Output is correct |
29 |
Correct |
13 ms |
15180 KB |
Output is correct |
30 |
Correct |
13 ms |
15308 KB |
Output is correct |
31 |
Correct |
14 ms |
15236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14356 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15752 KB |
Output is correct |
6 |
Correct |
14 ms |
15180 KB |
Output is correct |
7 |
Correct |
16 ms |
14968 KB |
Output is correct |
8 |
Correct |
16 ms |
15820 KB |
Output is correct |
9 |
Correct |
14 ms |
15180 KB |
Output is correct |
10 |
Correct |
13 ms |
14860 KB |
Output is correct |
11 |
Correct |
12 ms |
14668 KB |
Output is correct |
12 |
Correct |
13 ms |
15072 KB |
Output is correct |
13 |
Correct |
12 ms |
15052 KB |
Output is correct |
14 |
Correct |
13 ms |
14796 KB |
Output is correct |
15 |
Correct |
12 ms |
14836 KB |
Output is correct |
16 |
Correct |
16 ms |
15820 KB |
Output is correct |
17 |
Correct |
14 ms |
15308 KB |
Output is correct |
18 |
Correct |
12 ms |
14640 KB |
Output is correct |
19 |
Correct |
14 ms |
15052 KB |
Output is correct |
20 |
Correct |
13 ms |
14952 KB |
Output is correct |
21 |
Correct |
13 ms |
14940 KB |
Output is correct |
22 |
Correct |
16 ms |
15148 KB |
Output is correct |
23 |
Correct |
12 ms |
14796 KB |
Output is correct |
24 |
Correct |
15 ms |
15108 KB |
Output is correct |
25 |
Correct |
14 ms |
14948 KB |
Output is correct |
26 |
Correct |
16 ms |
15356 KB |
Output is correct |
27 |
Correct |
14 ms |
15176 KB |
Output is correct |
28 |
Correct |
13 ms |
15180 KB |
Output is correct |
29 |
Correct |
13 ms |
15180 KB |
Output is correct |
30 |
Correct |
13 ms |
15308 KB |
Output is correct |
31 |
Correct |
14 ms |
15236 KB |
Output is correct |
32 |
Correct |
17 ms |
15812 KB |
Output is correct |
33 |
Correct |
472 ms |
90396 KB |
Output is correct |
34 |
Correct |
356 ms |
59936 KB |
Output is correct |
35 |
Correct |
444 ms |
89744 KB |
Output is correct |
36 |
Correct |
355 ms |
59768 KB |
Output is correct |
37 |
Correct |
212 ms |
33352 KB |
Output is correct |
38 |
Correct |
202 ms |
27844 KB |
Output is correct |
39 |
Correct |
172 ms |
40268 KB |
Output is correct |
40 |
Correct |
148 ms |
40260 KB |
Output is correct |
41 |
Correct |
115 ms |
40216 KB |
Output is correct |
42 |
Correct |
157 ms |
32468 KB |
Output is correct |
43 |
Correct |
166 ms |
32392 KB |
Output is correct |
44 |
Correct |
368 ms |
100916 KB |
Output is correct |
45 |
Correct |
262 ms |
65296 KB |
Output is correct |
46 |
Correct |
115 ms |
27616 KB |
Output is correct |
47 |
Correct |
225 ms |
42500 KB |
Output is correct |
48 |
Correct |
137 ms |
35524 KB |
Output is correct |
49 |
Correct |
137 ms |
35524 KB |
Output is correct |
50 |
Correct |
306 ms |
44048 KB |
Output is correct |
51 |
Correct |
117 ms |
31688 KB |
Output is correct |
52 |
Correct |
344 ms |
43272 KB |
Output is correct |
53 |
Correct |
138 ms |
36160 KB |
Output is correct |
54 |
Correct |
187 ms |
52692 KB |
Output is correct |
55 |
Correct |
216 ms |
47540 KB |
Output is correct |
56 |
Correct |
236 ms |
47988 KB |
Output is correct |
57 |
Correct |
181 ms |
49484 KB |
Output is correct |
58 |
Correct |
205 ms |
51140 KB |
Output is correct |
59 |
Correct |
205 ms |
51084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14356 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15752 KB |
Output is correct |
6 |
Correct |
14 ms |
15180 KB |
Output is correct |
7 |
Correct |
16 ms |
14968 KB |
Output is correct |
8 |
Correct |
16 ms |
15820 KB |
Output is correct |
9 |
Correct |
14 ms |
15180 KB |
Output is correct |
10 |
Correct |
13 ms |
14860 KB |
Output is correct |
11 |
Correct |
12 ms |
14668 KB |
Output is correct |
12 |
Correct |
13 ms |
15072 KB |
Output is correct |
13 |
Correct |
12 ms |
15052 KB |
Output is correct |
14 |
Correct |
13 ms |
14796 KB |
Output is correct |
15 |
Correct |
12 ms |
14836 KB |
Output is correct |
16 |
Correct |
16 ms |
15820 KB |
Output is correct |
17 |
Correct |
14 ms |
15308 KB |
Output is correct |
18 |
Correct |
12 ms |
14640 KB |
Output is correct |
19 |
Correct |
14 ms |
15052 KB |
Output is correct |
20 |
Correct |
13 ms |
14952 KB |
Output is correct |
21 |
Correct |
13 ms |
14940 KB |
Output is correct |
22 |
Correct |
16 ms |
15148 KB |
Output is correct |
23 |
Correct |
12 ms |
14796 KB |
Output is correct |
24 |
Correct |
15 ms |
15108 KB |
Output is correct |
25 |
Correct |
14 ms |
14948 KB |
Output is correct |
26 |
Correct |
16 ms |
15356 KB |
Output is correct |
27 |
Correct |
14 ms |
15176 KB |
Output is correct |
28 |
Correct |
13 ms |
15180 KB |
Output is correct |
29 |
Correct |
13 ms |
15180 KB |
Output is correct |
30 |
Correct |
13 ms |
15308 KB |
Output is correct |
31 |
Correct |
14 ms |
15236 KB |
Output is correct |
32 |
Correct |
17 ms |
15812 KB |
Output is correct |
33 |
Correct |
472 ms |
90396 KB |
Output is correct |
34 |
Correct |
356 ms |
59936 KB |
Output is correct |
35 |
Correct |
444 ms |
89744 KB |
Output is correct |
36 |
Correct |
355 ms |
59768 KB |
Output is correct |
37 |
Correct |
212 ms |
33352 KB |
Output is correct |
38 |
Correct |
202 ms |
27844 KB |
Output is correct |
39 |
Correct |
172 ms |
40268 KB |
Output is correct |
40 |
Correct |
148 ms |
40260 KB |
Output is correct |
41 |
Correct |
115 ms |
40216 KB |
Output is correct |
42 |
Correct |
157 ms |
32468 KB |
Output is correct |
43 |
Correct |
166 ms |
32392 KB |
Output is correct |
44 |
Correct |
368 ms |
100916 KB |
Output is correct |
45 |
Correct |
262 ms |
65296 KB |
Output is correct |
46 |
Correct |
115 ms |
27616 KB |
Output is correct |
47 |
Correct |
225 ms |
42500 KB |
Output is correct |
48 |
Correct |
137 ms |
35524 KB |
Output is correct |
49 |
Correct |
137 ms |
35524 KB |
Output is correct |
50 |
Correct |
306 ms |
44048 KB |
Output is correct |
51 |
Correct |
117 ms |
31688 KB |
Output is correct |
52 |
Correct |
344 ms |
43272 KB |
Output is correct |
53 |
Correct |
138 ms |
36160 KB |
Output is correct |
54 |
Correct |
187 ms |
52692 KB |
Output is correct |
55 |
Correct |
216 ms |
47540 KB |
Output is correct |
56 |
Correct |
236 ms |
47988 KB |
Output is correct |
57 |
Correct |
181 ms |
49484 KB |
Output is correct |
58 |
Correct |
205 ms |
51140 KB |
Output is correct |
59 |
Correct |
205 ms |
51084 KB |
Output is correct |
60 |
Correct |
10 ms |
14412 KB |
Output is correct |
61 |
Correct |
10 ms |
14412 KB |
Output is correct |
62 |
Correct |
9 ms |
14412 KB |
Output is correct |
63 |
Correct |
10 ms |
14412 KB |
Output is correct |
64 |
Correct |
377 ms |
62164 KB |
Output is correct |
65 |
Correct |
312 ms |
45372 KB |
Output is correct |
66 |
Correct |
380 ms |
67828 KB |
Output is correct |
67 |
Correct |
354 ms |
50472 KB |
Output is correct |
68 |
Correct |
273 ms |
35884 KB |
Output is correct |
69 |
Correct |
231 ms |
32108 KB |
Output is correct |
70 |
Correct |
228 ms |
40436 KB |
Output is correct |
71 |
Correct |
184 ms |
37740 KB |
Output is correct |
72 |
Correct |
238 ms |
46096 KB |
Output is correct |
73 |
Correct |
187 ms |
46160 KB |
Output is correct |
74 |
Correct |
331 ms |
49120 KB |
Output is correct |
75 |
Correct |
229 ms |
41488 KB |
Output is correct |
76 |
Correct |
197 ms |
41408 KB |
Output is correct |
77 |
Correct |
257 ms |
49664 KB |
Output is correct |
78 |
Correct |
201 ms |
41796 KB |
Output is correct |
79 |
Correct |
363 ms |
69240 KB |
Output is correct |
80 |
Correct |
275 ms |
55440 KB |
Output is correct |
81 |
Correct |
240 ms |
42344 KB |
Output is correct |
82 |
Correct |
294 ms |
58816 KB |
Output is correct |
83 |
Correct |
115 ms |
35876 KB |
Output is correct |
84 |
Correct |
414 ms |
49120 KB |
Output is correct |
85 |
Correct |
304 ms |
49100 KB |
Output is correct |
86 |
Correct |
299 ms |
49040 KB |
Output is correct |
87 |
Correct |
300 ms |
49096 KB |
Output is correct |
88 |
Correct |
310 ms |
49104 KB |
Output is correct |