#include <bits/stdc++.h>
#define long long long
#define x first
#define y second
using namespace std;
const int N = 2e5 + 5;
int n;
int A[N], H[N], C[N], indeg[N];
map<int, long> f[N];
int main() {
scanf("%d", &n);
vector<int> coord;
for(int i = 1; i <= n; i++) {
scanf("%d %d %d", A + i, H + i, C + i);
coord.emplace_back(H[i]);
++indeg[A[i]];
}
sort(coord.begin(), coord.end());
coord.resize(unique(coord.begin(), coord.end()) - coord.begin());
for(int i = 1; i <= n; i++)
H[i] = lower_bound(coord.begin(), coord.end(), H[i]) - coord.begin();
vector<int> chk(n + 1);
queue<int> Q;
for(int i = 1; i <= n; i++) if(!indeg[i])
Q.emplace(i), chk[i] = 1;
while(!Q.empty()) {
int u = Q.front(); Q.pop();
f[u][H[u] + 1] += C[u];
long sum = 0;
bool valid = false;
while(f[u].begin()->x != H[u] + 1) {
auto it = prev(f[u].find(H[u] + 1));
auto p = *it;
f[u].erase(it);
sum += p.y;
if(C[u] < sum) {
f[u][p.x] += sum - C[u];
f[u][0] += C[u];
valid = true;
break;
}
}
if(!valid)
f[u][0] += sum;
if(f[A[u]].size() < f[u].size())
swap(f[A[u]], f[u]);
for(auto p : f[u])
f[A[u]][p.x] += p.y;
f[u].clear();
if(!--indeg[A[u]])
Q.emplace(A[u]), chk[A[u]] = 1;
}
long ans = 0;
for(int i = 1; i <= n; i++) if(!chk[i]) {
vector<int> cyc = {i};
while(A[cyc.back()] != i)
cyc.emplace_back(A[cyc.back()]);
long sum_cost = 0;
map<int, long> f_cyc, sub;
for(int u : cyc) {
chk[u] = 1;
sum_cost += C[u];
sub[H[u]] += C[u];
if(f_cyc.size() < f[u].size())
swap(f_cyc, f[u]);
for(auto p : f[u])
f_cyc[p.x] += p.y;
}
for(auto p : sub) if(!f_cyc.count(p.x))
f_cyc[p.x] = 0;
long pref = 0, ret = 1e18;
for(auto p : f_cyc) {
pref += f_cyc[p.x];
f_cyc[p.x] = pref;
}
for(auto p : f_cyc)
ret = min(ret, sum_cost - sub[p.x] + f_cyc[p.x]);
ans += ret;
}
printf("%lld\n", ans);
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
worst_reporter2.cpp:20:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d %d %d", A + i, H + i, C + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
14 ms |
10344 KB |
Output is correct |
6 |
Correct |
11 ms |
10132 KB |
Output is correct |
7 |
Correct |
11 ms |
10104 KB |
Output is correct |
8 |
Correct |
14 ms |
10348 KB |
Output is correct |
9 |
Correct |
11 ms |
10232 KB |
Output is correct |
10 |
Correct |
10 ms |
10188 KB |
Output is correct |
11 |
Correct |
9 ms |
10096 KB |
Output is correct |
12 |
Correct |
11 ms |
9896 KB |
Output is correct |
13 |
Correct |
9 ms |
9964 KB |
Output is correct |
14 |
Correct |
11 ms |
9932 KB |
Output is correct |
15 |
Correct |
11 ms |
9932 KB |
Output is correct |
16 |
Correct |
14 ms |
10344 KB |
Output is correct |
17 |
Correct |
10 ms |
10060 KB |
Output is correct |
18 |
Correct |
9 ms |
10060 KB |
Output is correct |
19 |
Correct |
12 ms |
10188 KB |
Output is correct |
20 |
Correct |
10 ms |
10260 KB |
Output is correct |
21 |
Correct |
9 ms |
10188 KB |
Output is correct |
22 |
Correct |
13 ms |
10500 KB |
Output is correct |
23 |
Correct |
9 ms |
9836 KB |
Output is correct |
24 |
Correct |
13 ms |
10188 KB |
Output is correct |
25 |
Correct |
10 ms |
9932 KB |
Output is correct |
26 |
Correct |
9 ms |
9948 KB |
Output is correct |
27 |
Correct |
12 ms |
10316 KB |
Output is correct |
28 |
Correct |
12 ms |
10296 KB |
Output is correct |
29 |
Correct |
12 ms |
10340 KB |
Output is correct |
30 |
Correct |
12 ms |
10444 KB |
Output is correct |
31 |
Correct |
13 ms |
10444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
14 ms |
10344 KB |
Output is correct |
6 |
Correct |
11 ms |
10132 KB |
Output is correct |
7 |
Correct |
11 ms |
10104 KB |
Output is correct |
8 |
Correct |
14 ms |
10348 KB |
Output is correct |
9 |
Correct |
11 ms |
10232 KB |
Output is correct |
10 |
Correct |
10 ms |
10188 KB |
Output is correct |
11 |
Correct |
9 ms |
10096 KB |
Output is correct |
12 |
Correct |
11 ms |
9896 KB |
Output is correct |
13 |
Correct |
9 ms |
9964 KB |
Output is correct |
14 |
Correct |
11 ms |
9932 KB |
Output is correct |
15 |
Correct |
11 ms |
9932 KB |
Output is correct |
16 |
Correct |
14 ms |
10344 KB |
Output is correct |
17 |
Correct |
10 ms |
10060 KB |
Output is correct |
18 |
Correct |
9 ms |
10060 KB |
Output is correct |
19 |
Correct |
12 ms |
10188 KB |
Output is correct |
20 |
Correct |
10 ms |
10260 KB |
Output is correct |
21 |
Correct |
9 ms |
10188 KB |
Output is correct |
22 |
Correct |
13 ms |
10500 KB |
Output is correct |
23 |
Correct |
9 ms |
9836 KB |
Output is correct |
24 |
Correct |
13 ms |
10188 KB |
Output is correct |
25 |
Correct |
10 ms |
9932 KB |
Output is correct |
26 |
Correct |
9 ms |
9948 KB |
Output is correct |
27 |
Correct |
12 ms |
10316 KB |
Output is correct |
28 |
Correct |
12 ms |
10296 KB |
Output is correct |
29 |
Correct |
12 ms |
10340 KB |
Output is correct |
30 |
Correct |
12 ms |
10444 KB |
Output is correct |
31 |
Correct |
13 ms |
10444 KB |
Output is correct |
32 |
Correct |
14 ms |
10344 KB |
Output is correct |
33 |
Correct |
698 ms |
38808 KB |
Output is correct |
34 |
Correct |
385 ms |
30552 KB |
Output is correct |
35 |
Correct |
642 ms |
38764 KB |
Output is correct |
36 |
Correct |
370 ms |
30684 KB |
Output is correct |
37 |
Correct |
219 ms |
30152 KB |
Output is correct |
38 |
Correct |
173 ms |
28688 KB |
Output is correct |
39 |
Correct |
235 ms |
19652 KB |
Output is correct |
40 |
Correct |
193 ms |
19640 KB |
Output is correct |
41 |
Correct |
114 ms |
19640 KB |
Output is correct |
42 |
Correct |
232 ms |
19744 KB |
Output is correct |
43 |
Correct |
199 ms |
19600 KB |
Output is correct |
44 |
Correct |
525 ms |
37440 KB |
Output is correct |
45 |
Correct |
271 ms |
28980 KB |
Output is correct |
46 |
Correct |
113 ms |
25476 KB |
Output is correct |
47 |
Correct |
379 ms |
33208 KB |
Output is correct |
48 |
Correct |
187 ms |
31452 KB |
Output is correct |
49 |
Correct |
127 ms |
31560 KB |
Output is correct |
50 |
Correct |
402 ms |
42968 KB |
Output is correct |
51 |
Correct |
147 ms |
18880 KB |
Output is correct |
52 |
Correct |
356 ms |
33536 KB |
Output is correct |
53 |
Correct |
162 ms |
19512 KB |
Output is correct |
54 |
Correct |
130 ms |
19788 KB |
Output is correct |
55 |
Correct |
317 ms |
36532 KB |
Output is correct |
56 |
Correct |
282 ms |
37920 KB |
Output is correct |
57 |
Correct |
283 ms |
39760 KB |
Output is correct |
58 |
Correct |
431 ms |
43780 KB |
Output is correct |
59 |
Correct |
433 ms |
43876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
14 ms |
10344 KB |
Output is correct |
6 |
Correct |
11 ms |
10132 KB |
Output is correct |
7 |
Correct |
11 ms |
10104 KB |
Output is correct |
8 |
Correct |
14 ms |
10348 KB |
Output is correct |
9 |
Correct |
11 ms |
10232 KB |
Output is correct |
10 |
Correct |
10 ms |
10188 KB |
Output is correct |
11 |
Correct |
9 ms |
10096 KB |
Output is correct |
12 |
Correct |
11 ms |
9896 KB |
Output is correct |
13 |
Correct |
9 ms |
9964 KB |
Output is correct |
14 |
Correct |
11 ms |
9932 KB |
Output is correct |
15 |
Correct |
11 ms |
9932 KB |
Output is correct |
16 |
Correct |
14 ms |
10344 KB |
Output is correct |
17 |
Correct |
10 ms |
10060 KB |
Output is correct |
18 |
Correct |
9 ms |
10060 KB |
Output is correct |
19 |
Correct |
12 ms |
10188 KB |
Output is correct |
20 |
Correct |
10 ms |
10260 KB |
Output is correct |
21 |
Correct |
9 ms |
10188 KB |
Output is correct |
22 |
Correct |
13 ms |
10500 KB |
Output is correct |
23 |
Correct |
9 ms |
9836 KB |
Output is correct |
24 |
Correct |
13 ms |
10188 KB |
Output is correct |
25 |
Correct |
10 ms |
9932 KB |
Output is correct |
26 |
Correct |
9 ms |
9948 KB |
Output is correct |
27 |
Correct |
12 ms |
10316 KB |
Output is correct |
28 |
Correct |
12 ms |
10296 KB |
Output is correct |
29 |
Correct |
12 ms |
10340 KB |
Output is correct |
30 |
Correct |
12 ms |
10444 KB |
Output is correct |
31 |
Correct |
13 ms |
10444 KB |
Output is correct |
32 |
Correct |
14 ms |
10344 KB |
Output is correct |
33 |
Correct |
698 ms |
38808 KB |
Output is correct |
34 |
Correct |
385 ms |
30552 KB |
Output is correct |
35 |
Correct |
642 ms |
38764 KB |
Output is correct |
36 |
Correct |
370 ms |
30684 KB |
Output is correct |
37 |
Correct |
219 ms |
30152 KB |
Output is correct |
38 |
Correct |
173 ms |
28688 KB |
Output is correct |
39 |
Correct |
235 ms |
19652 KB |
Output is correct |
40 |
Correct |
193 ms |
19640 KB |
Output is correct |
41 |
Correct |
114 ms |
19640 KB |
Output is correct |
42 |
Correct |
232 ms |
19744 KB |
Output is correct |
43 |
Correct |
199 ms |
19600 KB |
Output is correct |
44 |
Correct |
525 ms |
37440 KB |
Output is correct |
45 |
Correct |
271 ms |
28980 KB |
Output is correct |
46 |
Correct |
113 ms |
25476 KB |
Output is correct |
47 |
Correct |
379 ms |
33208 KB |
Output is correct |
48 |
Correct |
187 ms |
31452 KB |
Output is correct |
49 |
Correct |
127 ms |
31560 KB |
Output is correct |
50 |
Correct |
402 ms |
42968 KB |
Output is correct |
51 |
Correct |
147 ms |
18880 KB |
Output is correct |
52 |
Correct |
356 ms |
33536 KB |
Output is correct |
53 |
Correct |
162 ms |
19512 KB |
Output is correct |
54 |
Correct |
130 ms |
19788 KB |
Output is correct |
55 |
Correct |
317 ms |
36532 KB |
Output is correct |
56 |
Correct |
282 ms |
37920 KB |
Output is correct |
57 |
Correct |
283 ms |
39760 KB |
Output is correct |
58 |
Correct |
431 ms |
43780 KB |
Output is correct |
59 |
Correct |
433 ms |
43876 KB |
Output is correct |
60 |
Correct |
6 ms |
9676 KB |
Output is correct |
61 |
Correct |
6 ms |
9676 KB |
Output is correct |
62 |
Correct |
6 ms |
9676 KB |
Output is correct |
63 |
Correct |
6 ms |
9696 KB |
Output is correct |
64 |
Correct |
559 ms |
36280 KB |
Output is correct |
65 |
Correct |
381 ms |
28940 KB |
Output is correct |
66 |
Correct |
557 ms |
37288 KB |
Output is correct |
67 |
Correct |
338 ms |
28896 KB |
Output is correct |
68 |
Correct |
237 ms |
28084 KB |
Output is correct |
69 |
Correct |
194 ms |
27320 KB |
Output is correct |
70 |
Correct |
429 ms |
37308 KB |
Output is correct |
71 |
Correct |
141 ms |
20340 KB |
Output is correct |
72 |
Correct |
493 ms |
45276 KB |
Output is correct |
73 |
Correct |
148 ms |
21044 KB |
Output is correct |
74 |
Correct |
457 ms |
51200 KB |
Output is correct |
75 |
Correct |
198 ms |
32952 KB |
Output is correct |
76 |
Correct |
161 ms |
32916 KB |
Output is correct |
77 |
Correct |
352 ms |
38752 KB |
Output is correct |
78 |
Correct |
154 ms |
20596 KB |
Output is correct |
79 |
Correct |
457 ms |
37324 KB |
Output is correct |
80 |
Correct |
240 ms |
25916 KB |
Output is correct |
81 |
Correct |
175 ms |
24816 KB |
Output is correct |
82 |
Correct |
367 ms |
45504 KB |
Output is correct |
83 |
Correct |
153 ms |
19548 KB |
Output is correct |
84 |
Correct |
319 ms |
31896 KB |
Output is correct |
85 |
Correct |
329 ms |
31884 KB |
Output is correct |
86 |
Correct |
318 ms |
31912 KB |
Output is correct |
87 |
Correct |
328 ms |
31932 KB |
Output is correct |
88 |
Correct |
340 ms |
31940 KB |
Output is correct |