#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];
map<int, ll> t;
for (int i: d[v]){
t[h[i]] += c[i];
}
add(v, 1, cc, S);
for (auto [i, S]: t){
auto it = mp[v].upper_bound(i);
if (it == mp[v].begin()) continue;
it--;
if (i < cc) mp[v][i + 1] += S;
ll x = S; 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]){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
6 ms |
2652 KB |
Output is correct |
7 |
Correct |
6 ms |
2652 KB |
Output is correct |
8 |
Correct |
6 ms |
2652 KB |
Output is correct |
9 |
Correct |
7 ms |
2652 KB |
Output is correct |
10 |
Correct |
7 ms |
2824 KB |
Output is correct |
11 |
Correct |
6 ms |
2908 KB |
Output is correct |
12 |
Correct |
4 ms |
2908 KB |
Output is correct |
13 |
Correct |
5 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
2140 KB |
Output is correct |
15 |
Correct |
4 ms |
2256 KB |
Output is correct |
16 |
Correct |
6 ms |
2908 KB |
Output is correct |
17 |
Correct |
6 ms |
3164 KB |
Output is correct |
18 |
Correct |
6 ms |
3420 KB |
Output is correct |
19 |
Correct |
5 ms |
2652 KB |
Output is correct |
20 |
Correct |
5 ms |
2636 KB |
Output is correct |
21 |
Correct |
5 ms |
2484 KB |
Output is correct |
22 |
Correct |
4 ms |
2392 KB |
Output is correct |
23 |
Correct |
4 ms |
2392 KB |
Output is correct |
24 |
Correct |
4 ms |
2648 KB |
Output is correct |
25 |
Correct |
5 ms |
2652 KB |
Output is correct |
26 |
Correct |
4 ms |
3160 KB |
Output is correct |
27 |
Correct |
4 ms |
2648 KB |
Output is correct |
28 |
Correct |
7 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
2908 KB |
Output is correct |
30 |
Correct |
5 ms |
2672 KB |
Output is correct |
31 |
Correct |
4 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
6 ms |
2652 KB |
Output is correct |
7 |
Correct |
6 ms |
2652 KB |
Output is correct |
8 |
Correct |
6 ms |
2652 KB |
Output is correct |
9 |
Correct |
7 ms |
2652 KB |
Output is correct |
10 |
Correct |
7 ms |
2824 KB |
Output is correct |
11 |
Correct |
6 ms |
2908 KB |
Output is correct |
12 |
Correct |
4 ms |
2908 KB |
Output is correct |
13 |
Correct |
5 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
2140 KB |
Output is correct |
15 |
Correct |
4 ms |
2256 KB |
Output is correct |
16 |
Correct |
6 ms |
2908 KB |
Output is correct |
17 |
Correct |
6 ms |
3164 KB |
Output is correct |
18 |
Correct |
6 ms |
3420 KB |
Output is correct |
19 |
Correct |
5 ms |
2652 KB |
Output is correct |
20 |
Correct |
5 ms |
2636 KB |
Output is correct |
21 |
Correct |
5 ms |
2484 KB |
Output is correct |
22 |
Correct |
4 ms |
2392 KB |
Output is correct |
23 |
Correct |
4 ms |
2392 KB |
Output is correct |
24 |
Correct |
4 ms |
2648 KB |
Output is correct |
25 |
Correct |
5 ms |
2652 KB |
Output is correct |
26 |
Correct |
4 ms |
3160 KB |
Output is correct |
27 |
Correct |
4 ms |
2648 KB |
Output is correct |
28 |
Correct |
7 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
2908 KB |
Output is correct |
30 |
Correct |
5 ms |
2672 KB |
Output is correct |
31 |
Correct |
4 ms |
2652 KB |
Output is correct |
32 |
Correct |
6 ms |
2648 KB |
Output is correct |
33 |
Correct |
390 ms |
108508 KB |
Output is correct |
34 |
Correct |
376 ms |
108236 KB |
Output is correct |
35 |
Correct |
379 ms |
105528 KB |
Output is correct |
36 |
Correct |
387 ms |
108272 KB |
Output is correct |
37 |
Correct |
402 ms |
109772 KB |
Output is correct |
38 |
Correct |
435 ms |
120264 KB |
Output is correct |
39 |
Correct |
184 ms |
98832 KB |
Output is correct |
40 |
Correct |
186 ms |
98772 KB |
Output is correct |
41 |
Correct |
214 ms |
101232 KB |
Output is correct |
42 |
Correct |
168 ms |
76632 KB |
Output is correct |
43 |
Correct |
168 ms |
76488 KB |
Output is correct |
44 |
Correct |
344 ms |
133456 KB |
Output is correct |
45 |
Correct |
361 ms |
133688 KB |
Output is correct |
46 |
Correct |
429 ms |
162248 KB |
Output is correct |
47 |
Correct |
253 ms |
89548 KB |
Output is correct |
48 |
Correct |
257 ms |
88808 KB |
Output is correct |
49 |
Correct |
190 ms |
90056 KB |
Output is correct |
50 |
Correct |
189 ms |
79820 KB |
Output is correct |
51 |
Correct |
213 ms |
79836 KB |
Output is correct |
52 |
Correct |
212 ms |
90468 KB |
Output is correct |
53 |
Correct |
219 ms |
89708 KB |
Output is correct |
54 |
Correct |
190 ms |
111304 KB |
Output is correct |
55 |
Correct |
245 ms |
91344 KB |
Output is correct |
56 |
Correct |
200 ms |
99532 KB |
Output is correct |
57 |
Correct |
199 ms |
103628 KB |
Output is correct |
58 |
Correct |
191 ms |
94312 KB |
Output is correct |
59 |
Correct |
187 ms |
94440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
6 ms |
2652 KB |
Output is correct |
7 |
Correct |
6 ms |
2652 KB |
Output is correct |
8 |
Correct |
6 ms |
2652 KB |
Output is correct |
9 |
Correct |
7 ms |
2652 KB |
Output is correct |
10 |
Correct |
7 ms |
2824 KB |
Output is correct |
11 |
Correct |
6 ms |
2908 KB |
Output is correct |
12 |
Correct |
4 ms |
2908 KB |
Output is correct |
13 |
Correct |
5 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
2140 KB |
Output is correct |
15 |
Correct |
4 ms |
2256 KB |
Output is correct |
16 |
Correct |
6 ms |
2908 KB |
Output is correct |
17 |
Correct |
6 ms |
3164 KB |
Output is correct |
18 |
Correct |
6 ms |
3420 KB |
Output is correct |
19 |
Correct |
5 ms |
2652 KB |
Output is correct |
20 |
Correct |
5 ms |
2636 KB |
Output is correct |
21 |
Correct |
5 ms |
2484 KB |
Output is correct |
22 |
Correct |
4 ms |
2392 KB |
Output is correct |
23 |
Correct |
4 ms |
2392 KB |
Output is correct |
24 |
Correct |
4 ms |
2648 KB |
Output is correct |
25 |
Correct |
5 ms |
2652 KB |
Output is correct |
26 |
Correct |
4 ms |
3160 KB |
Output is correct |
27 |
Correct |
4 ms |
2648 KB |
Output is correct |
28 |
Correct |
7 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
2908 KB |
Output is correct |
30 |
Correct |
5 ms |
2672 KB |
Output is correct |
31 |
Correct |
4 ms |
2652 KB |
Output is correct |
32 |
Correct |
6 ms |
2648 KB |
Output is correct |
33 |
Correct |
390 ms |
108508 KB |
Output is correct |
34 |
Correct |
376 ms |
108236 KB |
Output is correct |
35 |
Correct |
379 ms |
105528 KB |
Output is correct |
36 |
Correct |
387 ms |
108272 KB |
Output is correct |
37 |
Correct |
402 ms |
109772 KB |
Output is correct |
38 |
Correct |
435 ms |
120264 KB |
Output is correct |
39 |
Correct |
184 ms |
98832 KB |
Output is correct |
40 |
Correct |
186 ms |
98772 KB |
Output is correct |
41 |
Correct |
214 ms |
101232 KB |
Output is correct |
42 |
Correct |
168 ms |
76632 KB |
Output is correct |
43 |
Correct |
168 ms |
76488 KB |
Output is correct |
44 |
Correct |
344 ms |
133456 KB |
Output is correct |
45 |
Correct |
361 ms |
133688 KB |
Output is correct |
46 |
Correct |
429 ms |
162248 KB |
Output is correct |
47 |
Correct |
253 ms |
89548 KB |
Output is correct |
48 |
Correct |
257 ms |
88808 KB |
Output is correct |
49 |
Correct |
190 ms |
90056 KB |
Output is correct |
50 |
Correct |
189 ms |
79820 KB |
Output is correct |
51 |
Correct |
213 ms |
79836 KB |
Output is correct |
52 |
Correct |
212 ms |
90468 KB |
Output is correct |
53 |
Correct |
219 ms |
89708 KB |
Output is correct |
54 |
Correct |
190 ms |
111304 KB |
Output is correct |
55 |
Correct |
245 ms |
91344 KB |
Output is correct |
56 |
Correct |
200 ms |
99532 KB |
Output is correct |
57 |
Correct |
199 ms |
103628 KB |
Output is correct |
58 |
Correct |
191 ms |
94312 KB |
Output is correct |
59 |
Correct |
187 ms |
94440 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 |
344 KB |
Output is correct |
64 |
Correct |
373 ms |
92920 KB |
Output is correct |
65 |
Incorrect |
370 ms |
91852 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |