#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n;
int nxt[N], a[N], c[N], lab[N], deg[N];
vector<int> g[N];
map<int, long long> mp[N];
void mrg(map<int, long long> &a, map<int, long long> &b) {
if (a.size() < b.size()) {
swap(a, b);
}
for (auto [x, y] : b) {
a[x] += y;
}
}
void opt(map<int, long long> &mp, int x, long long C) {
mp[x] += C;
for (auto it = mp.upper_bound(x); it != mp.end(); it = mp.erase(it)) {
auto &delta = (*it).second;
if (C < delta) {
delta -= C;
break;
}
C -= delta;
}
}
void dfs(int u) {
for (int v : g[u]) {
dfs(v);
mrg(mp[u], mp[v]);
}
opt(mp[u], a[u], c[u]);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
long long res = 0;
for (int i = 1; i <= n; ++i) {
int p; cin >> p >> a[i] >> c[i];
a[i] = 1e9 - a[i] + 1;
res += c[i];
g[p].push_back(i);
nxt[i] = p, ++deg[p];
}
queue<int> q;
for (int i = 1; i <= n; ++i) {
if (!deg[i]) {
q.push(i);
}
}
while (q.size()) {
int u = q.front(); q.pop();
if (--deg[nxt[u]] == 0) {
q.push(nxt[u]);
}
}
for (int i = 1; i <= n; ++i) {
if (deg[i]) {
int j = i;
vector<int> cands;
do {
cands.push_back(j);
for (int v : g[j]) {
if (!deg[v]) {
dfs(v);
mrg(mp[i], mp[v]);
}
}
j = nxt[j];
} while (j ^ i);
sort(cands.begin(), cands.end(), [&](int u, int v) {
return a[u] > a[v];
});
for (int it = 0; it < cands.size(); ) {
int j = it;
long long sum = 0;
while (j < cands.size() && a[cands[j]] == a[cands[it]]) {
deg[cands[j]] = 0;
sum += c[cands[j++]];
}
opt(mp[i], a[cands[it]], sum);
it = j;
}
for (auto [x, y] : mp[i]) {
res -= y;
}
}
}
cout << res;
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:88:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int it = 0; it < cands.size(); ) {
| ~~~^~~~~~~~~~~~~~
worst_reporter2.cpp:91:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | while (j < cands.size() && a[cands[j]] == a[cands[it]]) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
11 ms |
15628 KB |
Output is correct |
6 |
Correct |
8 ms |
15216 KB |
Output is correct |
7 |
Correct |
7 ms |
15080 KB |
Output is correct |
8 |
Correct |
10 ms |
15708 KB |
Output is correct |
9 |
Correct |
7 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
14996 KB |
Output is correct |
11 |
Correct |
6 ms |
14940 KB |
Output is correct |
12 |
Correct |
7 ms |
15196 KB |
Output is correct |
13 |
Correct |
8 ms |
15196 KB |
Output is correct |
14 |
Correct |
7 ms |
14968 KB |
Output is correct |
15 |
Correct |
6 ms |
14940 KB |
Output is correct |
16 |
Correct |
8 ms |
15984 KB |
Output is correct |
17 |
Correct |
8 ms |
15452 KB |
Output is correct |
18 |
Correct |
6 ms |
14940 KB |
Output is correct |
19 |
Correct |
7 ms |
15080 KB |
Output is correct |
20 |
Correct |
7 ms |
14936 KB |
Output is correct |
21 |
Correct |
6 ms |
14936 KB |
Output is correct |
22 |
Correct |
8 ms |
15292 KB |
Output is correct |
23 |
Correct |
7 ms |
14940 KB |
Output is correct |
24 |
Correct |
10 ms |
15328 KB |
Output is correct |
25 |
Correct |
7 ms |
15112 KB |
Output is correct |
26 |
Correct |
10 ms |
15196 KB |
Output is correct |
27 |
Correct |
8 ms |
15196 KB |
Output is correct |
28 |
Correct |
7 ms |
15336 KB |
Output is correct |
29 |
Correct |
7 ms |
15196 KB |
Output is correct |
30 |
Correct |
7 ms |
15340 KB |
Output is correct |
31 |
Correct |
7 ms |
15344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
11 ms |
15628 KB |
Output is correct |
6 |
Correct |
8 ms |
15216 KB |
Output is correct |
7 |
Correct |
7 ms |
15080 KB |
Output is correct |
8 |
Correct |
10 ms |
15708 KB |
Output is correct |
9 |
Correct |
7 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
14996 KB |
Output is correct |
11 |
Correct |
6 ms |
14940 KB |
Output is correct |
12 |
Correct |
7 ms |
15196 KB |
Output is correct |
13 |
Correct |
8 ms |
15196 KB |
Output is correct |
14 |
Correct |
7 ms |
14968 KB |
Output is correct |
15 |
Correct |
6 ms |
14940 KB |
Output is correct |
16 |
Correct |
8 ms |
15984 KB |
Output is correct |
17 |
Correct |
8 ms |
15452 KB |
Output is correct |
18 |
Correct |
6 ms |
14940 KB |
Output is correct |
19 |
Correct |
7 ms |
15080 KB |
Output is correct |
20 |
Correct |
7 ms |
14936 KB |
Output is correct |
21 |
Correct |
6 ms |
14936 KB |
Output is correct |
22 |
Correct |
8 ms |
15292 KB |
Output is correct |
23 |
Correct |
7 ms |
14940 KB |
Output is correct |
24 |
Correct |
10 ms |
15328 KB |
Output is correct |
25 |
Correct |
7 ms |
15112 KB |
Output is correct |
26 |
Correct |
10 ms |
15196 KB |
Output is correct |
27 |
Correct |
8 ms |
15196 KB |
Output is correct |
28 |
Correct |
7 ms |
15336 KB |
Output is correct |
29 |
Correct |
7 ms |
15196 KB |
Output is correct |
30 |
Correct |
7 ms |
15340 KB |
Output is correct |
31 |
Correct |
7 ms |
15344 KB |
Output is correct |
32 |
Correct |
8 ms |
15708 KB |
Output is correct |
33 |
Correct |
209 ms |
80144 KB |
Output is correct |
34 |
Correct |
154 ms |
55428 KB |
Output is correct |
35 |
Correct |
205 ms |
77140 KB |
Output is correct |
36 |
Correct |
187 ms |
55436 KB |
Output is correct |
37 |
Correct |
95 ms |
36436 KB |
Output is correct |
38 |
Correct |
109 ms |
31884 KB |
Output is correct |
39 |
Correct |
81 ms |
41456 KB |
Output is correct |
40 |
Correct |
77 ms |
41368 KB |
Output is correct |
41 |
Correct |
62 ms |
41424 KB |
Output is correct |
42 |
Correct |
88 ms |
35240 KB |
Output is correct |
43 |
Correct |
73 ms |
35276 KB |
Output is correct |
44 |
Correct |
214 ms |
104720 KB |
Output is correct |
45 |
Correct |
141 ms |
69200 KB |
Output is correct |
46 |
Correct |
54 ms |
31652 KB |
Output is correct |
47 |
Correct |
126 ms |
44848 KB |
Output is correct |
48 |
Correct |
70 ms |
37976 KB |
Output is correct |
49 |
Correct |
73 ms |
37972 KB |
Output is correct |
50 |
Correct |
181 ms |
46792 KB |
Output is correct |
51 |
Correct |
57 ms |
34508 KB |
Output is correct |
52 |
Correct |
132 ms |
45436 KB |
Output is correct |
53 |
Correct |
88 ms |
38344 KB |
Output is correct |
54 |
Correct |
61 ms |
41552 KB |
Output is correct |
55 |
Correct |
104 ms |
46368 KB |
Output is correct |
56 |
Correct |
104 ms |
48468 KB |
Output is correct |
57 |
Correct |
84 ms |
50116 KB |
Output is correct |
58 |
Correct |
95 ms |
53384 KB |
Output is correct |
59 |
Correct |
93 ms |
53332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
11 ms |
15628 KB |
Output is correct |
6 |
Correct |
8 ms |
15216 KB |
Output is correct |
7 |
Correct |
7 ms |
15080 KB |
Output is correct |
8 |
Correct |
10 ms |
15708 KB |
Output is correct |
9 |
Correct |
7 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
14996 KB |
Output is correct |
11 |
Correct |
6 ms |
14940 KB |
Output is correct |
12 |
Correct |
7 ms |
15196 KB |
Output is correct |
13 |
Correct |
8 ms |
15196 KB |
Output is correct |
14 |
Correct |
7 ms |
14968 KB |
Output is correct |
15 |
Correct |
6 ms |
14940 KB |
Output is correct |
16 |
Correct |
8 ms |
15984 KB |
Output is correct |
17 |
Correct |
8 ms |
15452 KB |
Output is correct |
18 |
Correct |
6 ms |
14940 KB |
Output is correct |
19 |
Correct |
7 ms |
15080 KB |
Output is correct |
20 |
Correct |
7 ms |
14936 KB |
Output is correct |
21 |
Correct |
6 ms |
14936 KB |
Output is correct |
22 |
Correct |
8 ms |
15292 KB |
Output is correct |
23 |
Correct |
7 ms |
14940 KB |
Output is correct |
24 |
Correct |
10 ms |
15328 KB |
Output is correct |
25 |
Correct |
7 ms |
15112 KB |
Output is correct |
26 |
Correct |
10 ms |
15196 KB |
Output is correct |
27 |
Correct |
8 ms |
15196 KB |
Output is correct |
28 |
Correct |
7 ms |
15336 KB |
Output is correct |
29 |
Correct |
7 ms |
15196 KB |
Output is correct |
30 |
Correct |
7 ms |
15340 KB |
Output is correct |
31 |
Correct |
7 ms |
15344 KB |
Output is correct |
32 |
Correct |
8 ms |
15708 KB |
Output is correct |
33 |
Correct |
209 ms |
80144 KB |
Output is correct |
34 |
Correct |
154 ms |
55428 KB |
Output is correct |
35 |
Correct |
205 ms |
77140 KB |
Output is correct |
36 |
Correct |
187 ms |
55436 KB |
Output is correct |
37 |
Correct |
95 ms |
36436 KB |
Output is correct |
38 |
Correct |
109 ms |
31884 KB |
Output is correct |
39 |
Correct |
81 ms |
41456 KB |
Output is correct |
40 |
Correct |
77 ms |
41368 KB |
Output is correct |
41 |
Correct |
62 ms |
41424 KB |
Output is correct |
42 |
Correct |
88 ms |
35240 KB |
Output is correct |
43 |
Correct |
73 ms |
35276 KB |
Output is correct |
44 |
Correct |
214 ms |
104720 KB |
Output is correct |
45 |
Correct |
141 ms |
69200 KB |
Output is correct |
46 |
Correct |
54 ms |
31652 KB |
Output is correct |
47 |
Correct |
126 ms |
44848 KB |
Output is correct |
48 |
Correct |
70 ms |
37976 KB |
Output is correct |
49 |
Correct |
73 ms |
37972 KB |
Output is correct |
50 |
Correct |
181 ms |
46792 KB |
Output is correct |
51 |
Correct |
57 ms |
34508 KB |
Output is correct |
52 |
Correct |
132 ms |
45436 KB |
Output is correct |
53 |
Correct |
88 ms |
38344 KB |
Output is correct |
54 |
Correct |
61 ms |
41552 KB |
Output is correct |
55 |
Correct |
104 ms |
46368 KB |
Output is correct |
56 |
Correct |
104 ms |
48468 KB |
Output is correct |
57 |
Correct |
84 ms |
50116 KB |
Output is correct |
58 |
Correct |
95 ms |
53384 KB |
Output is correct |
59 |
Correct |
93 ms |
53332 KB |
Output is correct |
60 |
Correct |
5 ms |
14428 KB |
Output is correct |
61 |
Correct |
5 ms |
14428 KB |
Output is correct |
62 |
Correct |
4 ms |
14424 KB |
Output is correct |
63 |
Correct |
6 ms |
14424 KB |
Output is correct |
64 |
Correct |
201 ms |
65200 KB |
Output is correct |
65 |
Correct |
156 ms |
48464 KB |
Output is correct |
66 |
Correct |
184 ms |
64596 KB |
Output is correct |
67 |
Correct |
165 ms |
48468 KB |
Output is correct |
68 |
Correct |
100 ms |
34588 KB |
Output is correct |
69 |
Correct |
87 ms |
31316 KB |
Output is correct |
70 |
Correct |
86 ms |
30132 KB |
Output is correct |
71 |
Correct |
97 ms |
29728 KB |
Output is correct |
72 |
Correct |
86 ms |
30208 KB |
Output is correct |
73 |
Correct |
80 ms |
30236 KB |
Output is correct |
74 |
Correct |
180 ms |
40232 KB |
Output is correct |
75 |
Correct |
110 ms |
32552 KB |
Output is correct |
76 |
Correct |
87 ms |
32512 KB |
Output is correct |
77 |
Correct |
120 ms |
40648 KB |
Output is correct |
78 |
Correct |
73 ms |
33228 KB |
Output is correct |
79 |
Correct |
157 ms |
56060 KB |
Output is correct |
80 |
Correct |
118 ms |
43984 KB |
Output is correct |
81 |
Correct |
110 ms |
33504 KB |
Output is correct |
82 |
Correct |
79 ms |
30412 KB |
Output is correct |
83 |
Correct |
59 ms |
41512 KB |
Output is correct |
84 |
Correct |
123 ms |
39880 KB |
Output is correct |
85 |
Correct |
117 ms |
39884 KB |
Output is correct |
86 |
Correct |
121 ms |
40052 KB |
Output is correct |
87 |
Correct |
120 ms |
39880 KB |
Output is correct |
88 |
Correct |
124 ms |
39876 KB |
Output is correct |