#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> a(n + 1), h(n + 1), c(n + 1);
for (int i = 1; i <= n; i++){
cin>>a[i]>>h[i]>>c[i];
}
vector<pii> all;
for (int i = 1; i <= n; i++) all.pb({h[i], i});
sort(all.begin(), all.end());
int i = 0, cc = 0;
while (i < all.size()){
int j = i;
while (j < all.size() && all[i] == all[j]){
j++;
}
cc++;
while (i < j){
h[all[i].ss] = cc;
i++;
}
}
vector<int> g1[n + 1];
for (int i = 1; i <= n; i++){
if (a[i] != i) g1[a[i]].pb(i);
}
vector<bool> used(n + 1), seen(n + 1);
vector<int> dd(n + 1), d[n + 1], st;
function<void(int)> dfs = [&](int v){
seen[v] = used[v] = 1;
st.pb(v);
for (int i: g1[v]){
if (used[i]){
if (seen[i]){
for (int j: st){
dd[j] = st[0];
}
}
continue;
}
dfs(i);
}
seen[v] = 0; st.pop_back();
};
for (int i = 1; i <= n; i++){
if (!used[i]){
dfs(i);
}
}
vector<int> g[n + 1];
for (int i = 1; i <= n; i++){
if (!dd[i]) dd[i] = i;
d[dd[i]].pb(i);
}
fill(used.begin(), used.end(), 0);
for (int i = 1; i <= n; i++){
int x = dd[a[i]], y = dd[i];
if (x != y){
g[x].pb(y);
used[y] = 1;
}
}
vector<ll> sum(cc + 1);
map<int, ll> mp[n + 1];
auto add = [&](int v, int l, int r, ll x){
mp[v][l] += x;
if (r < cc) mp[v][r + 1] -= x;
};
function<void(int)> solve = [&](int v){
for (int i: g[v]) solve(i);
for (int i: g[v]){
if (mp[v].size() < mp[i].size()) swap(mp[v], mp[i]);
for (auto [p, x]: mp[i]){
mp[v][p] += x;
}
}
ll S = 0;
for (int i: d[v]) S += c[i];
vector<int> t;
for (int i: d[v]){
t.pb(h[i]);
sum[h[i]] = 0;
}
for (int i: d[v]) sum[h[i]] += c[i];
add(v, 1, cc, S);
sort(t.begin(), t.end());
for (int i: t){
auto it = mp[v].upper_bound(i);
if (it == mp[v].begin()) continue;
it--;
if (i < cc) mp[v][i + 1] += sum[i];
ll x = sum[i]; int r = -1;
while ((*it).ss < x) {
r = (*it).ff;
x -= (*it).ss;
if (it == mp[v].begin()) break;
it--;
}
it -> ss -= x;
if (r != -1){
while (true){
it = mp[v].lower_bound(r);
if (it == mp[v].end() || (*it).ff > i) break;
mp[v].erase(it);
}
}
}
};
ll out = 0;
for (int i = 1; i <= n; i++){
if (!used[i]){
solve(i);
out += (*mp[i].begin()).ss;
}
}
cout<<out<<"\n";
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:25:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | while (i < all.size()){
| ~~^~~~~~~~~~~~
worst_reporter2.cpp:27:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | while (j < all.size() && all[i] == all[j]){
| ~~^~~~~~~~~~~~
# |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
7 ms |
2652 KB |
Output is correct |
7 |
Correct |
6 ms |
2652 KB |
Output is correct |
8 |
Correct |
6 ms |
2820 KB |
Output is correct |
9 |
Correct |
7 ms |
2648 KB |
Output is correct |
10 |
Correct |
7 ms |
2652 KB |
Output is correct |
11 |
Correct |
7 ms |
2932 KB |
Output is correct |
12 |
Correct |
4 ms |
2652 KB |
Output is correct |
13 |
Correct |
4 ms |
2652 KB |
Output is correct |
14 |
Correct |
4 ms |
2140 KB |
Output is correct |
15 |
Correct |
4 ms |
2252 KB |
Output is correct |
16 |
Correct |
6 ms |
2900 KB |
Output is correct |
17 |
Correct |
6 ms |
3164 KB |
Output is correct |
18 |
Correct |
7 ms |
3420 KB |
Output is correct |
19 |
Correct |
6 ms |
2424 KB |
Output is correct |
20 |
Correct |
5 ms |
2324 KB |
Output is correct |
21 |
Correct |
5 ms |
2396 KB |
Output is correct |
22 |
Correct |
4 ms |
2276 KB |
Output is correct |
23 |
Correct |
4 ms |
2396 KB |
Output is correct |
24 |
Correct |
4 ms |
2420 KB |
Output is correct |
25 |
Correct |
5 ms |
2396 KB |
Output is correct |
26 |
Correct |
5 ms |
2952 KB |
Output is correct |
27 |
Correct |
5 ms |
2396 KB |
Output is correct |
28 |
Correct |
5 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
2652 KB |
Output is correct |
30 |
Correct |
4 ms |
2648 KB |
Output is correct |
31 |
Correct |
4 ms |
2652 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
7 ms |
2652 KB |
Output is correct |
7 |
Correct |
6 ms |
2652 KB |
Output is correct |
8 |
Correct |
6 ms |
2820 KB |
Output is correct |
9 |
Correct |
7 ms |
2648 KB |
Output is correct |
10 |
Correct |
7 ms |
2652 KB |
Output is correct |
11 |
Correct |
7 ms |
2932 KB |
Output is correct |
12 |
Correct |
4 ms |
2652 KB |
Output is correct |
13 |
Correct |
4 ms |
2652 KB |
Output is correct |
14 |
Correct |
4 ms |
2140 KB |
Output is correct |
15 |
Correct |
4 ms |
2252 KB |
Output is correct |
16 |
Correct |
6 ms |
2900 KB |
Output is correct |
17 |
Correct |
6 ms |
3164 KB |
Output is correct |
18 |
Correct |
7 ms |
3420 KB |
Output is correct |
19 |
Correct |
6 ms |
2424 KB |
Output is correct |
20 |
Correct |
5 ms |
2324 KB |
Output is correct |
21 |
Correct |
5 ms |
2396 KB |
Output is correct |
22 |
Correct |
4 ms |
2276 KB |
Output is correct |
23 |
Correct |
4 ms |
2396 KB |
Output is correct |
24 |
Correct |
4 ms |
2420 KB |
Output is correct |
25 |
Correct |
5 ms |
2396 KB |
Output is correct |
26 |
Correct |
5 ms |
2952 KB |
Output is correct |
27 |
Correct |
5 ms |
2396 KB |
Output is correct |
28 |
Correct |
5 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
2652 KB |
Output is correct |
30 |
Correct |
4 ms |
2648 KB |
Output is correct |
31 |
Correct |
4 ms |
2652 KB |
Output is correct |
32 |
Correct |
6 ms |
2648 KB |
Output is correct |
33 |
Correct |
378 ms |
108492 KB |
Output is correct |
34 |
Correct |
392 ms |
108236 KB |
Output is correct |
35 |
Correct |
389 ms |
105452 KB |
Output is correct |
36 |
Correct |
390 ms |
108232 KB |
Output is correct |
37 |
Correct |
387 ms |
109672 KB |
Output is correct |
38 |
Correct |
410 ms |
120008 KB |
Output is correct |
39 |
Correct |
190 ms |
89396 KB |
Output is correct |
40 |
Correct |
188 ms |
89424 KB |
Output is correct |
41 |
Correct |
194 ms |
91884 KB |
Output is correct |
42 |
Correct |
165 ms |
71832 KB |
Output is correct |
43 |
Correct |
177 ms |
71896 KB |
Output is correct |
44 |
Correct |
363 ms |
133456 KB |
Output is correct |
45 |
Correct |
370 ms |
133592 KB |
Output is correct |
46 |
Correct |
375 ms |
162252 KB |
Output is correct |
47 |
Correct |
275 ms |
84688 KB |
Output is correct |
48 |
Correct |
265 ms |
84052 KB |
Output is correct |
49 |
Correct |
198 ms |
85308 KB |
Output is correct |
50 |
Correct |
209 ms |
79820 KB |
Output is correct |
51 |
Correct |
209 ms |
79668 KB |
Output is correct |
52 |
Correct |
230 ms |
85960 KB |
Output is correct |
53 |
Correct |
239 ms |
84940 KB |
Output is correct |
54 |
Correct |
193 ms |
102116 KB |
Output is correct |
55 |
Correct |
250 ms |
86476 KB |
Output is correct |
56 |
Correct |
220 ms |
92360 KB |
Output is correct |
57 |
Correct |
199 ms |
95432 KB |
Output is correct |
58 |
Correct |
193 ms |
89804 KB |
Output is correct |
59 |
Correct |
200 ms |
89836 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
7 ms |
2652 KB |
Output is correct |
7 |
Correct |
6 ms |
2652 KB |
Output is correct |
8 |
Correct |
6 ms |
2820 KB |
Output is correct |
9 |
Correct |
7 ms |
2648 KB |
Output is correct |
10 |
Correct |
7 ms |
2652 KB |
Output is correct |
11 |
Correct |
7 ms |
2932 KB |
Output is correct |
12 |
Correct |
4 ms |
2652 KB |
Output is correct |
13 |
Correct |
4 ms |
2652 KB |
Output is correct |
14 |
Correct |
4 ms |
2140 KB |
Output is correct |
15 |
Correct |
4 ms |
2252 KB |
Output is correct |
16 |
Correct |
6 ms |
2900 KB |
Output is correct |
17 |
Correct |
6 ms |
3164 KB |
Output is correct |
18 |
Correct |
7 ms |
3420 KB |
Output is correct |
19 |
Correct |
6 ms |
2424 KB |
Output is correct |
20 |
Correct |
5 ms |
2324 KB |
Output is correct |
21 |
Correct |
5 ms |
2396 KB |
Output is correct |
22 |
Correct |
4 ms |
2276 KB |
Output is correct |
23 |
Correct |
4 ms |
2396 KB |
Output is correct |
24 |
Correct |
4 ms |
2420 KB |
Output is correct |
25 |
Correct |
5 ms |
2396 KB |
Output is correct |
26 |
Correct |
5 ms |
2952 KB |
Output is correct |
27 |
Correct |
5 ms |
2396 KB |
Output is correct |
28 |
Correct |
5 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
2652 KB |
Output is correct |
30 |
Correct |
4 ms |
2648 KB |
Output is correct |
31 |
Correct |
4 ms |
2652 KB |
Output is correct |
32 |
Correct |
6 ms |
2648 KB |
Output is correct |
33 |
Correct |
378 ms |
108492 KB |
Output is correct |
34 |
Correct |
392 ms |
108236 KB |
Output is correct |
35 |
Correct |
389 ms |
105452 KB |
Output is correct |
36 |
Correct |
390 ms |
108232 KB |
Output is correct |
37 |
Correct |
387 ms |
109672 KB |
Output is correct |
38 |
Correct |
410 ms |
120008 KB |
Output is correct |
39 |
Correct |
190 ms |
89396 KB |
Output is correct |
40 |
Correct |
188 ms |
89424 KB |
Output is correct |
41 |
Correct |
194 ms |
91884 KB |
Output is correct |
42 |
Correct |
165 ms |
71832 KB |
Output is correct |
43 |
Correct |
177 ms |
71896 KB |
Output is correct |
44 |
Correct |
363 ms |
133456 KB |
Output is correct |
45 |
Correct |
370 ms |
133592 KB |
Output is correct |
46 |
Correct |
375 ms |
162252 KB |
Output is correct |
47 |
Correct |
275 ms |
84688 KB |
Output is correct |
48 |
Correct |
265 ms |
84052 KB |
Output is correct |
49 |
Correct |
198 ms |
85308 KB |
Output is correct |
50 |
Correct |
209 ms |
79820 KB |
Output is correct |
51 |
Correct |
209 ms |
79668 KB |
Output is correct |
52 |
Correct |
230 ms |
85960 KB |
Output is correct |
53 |
Correct |
239 ms |
84940 KB |
Output is correct |
54 |
Correct |
193 ms |
102116 KB |
Output is correct |
55 |
Correct |
250 ms |
86476 KB |
Output is correct |
56 |
Correct |
220 ms |
92360 KB |
Output is correct |
57 |
Correct |
199 ms |
95432 KB |
Output is correct |
58 |
Correct |
193 ms |
89804 KB |
Output is correct |
59 |
Correct |
200 ms |
89836 KB |
Output is correct |
60 |
Correct |
1 ms |
344 KB |
Output is correct |
61 |
Correct |
0 ms |
344 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
373 ms |
92776 KB |
Output is correct |
65 |
Incorrect |
368 ms |
91848 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |