#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
vector<int> h(n);
vector<int> c(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> h[i] >> c[i];
--a[i];
}
vector<vector<int>> g(n);
vector<int> deg(n);
for (int i = 0; i < n; i++) {
if (a[i] != i) {
g[a[i]].push_back(i);
}
deg[a[i]] += 1;
}
vector<int> que;
for (int i = 0; i < n; i++) {
if (deg[i] == 0) {
que.push_back(i);
}
}
for (int b = 0; b < (int) que.size(); b++) {
int i = que[b];
deg[a[i]] -= 1;
if (deg[a[i]] == 0) {
que.push_back(a[i]);
}
}
que.emplace_back(0);
vector<set<pair<int, long long>>> st(n);
for (int b = 0; b < (int) que.size(); b++) {
int i = que[b];
int ch = -1;
for (int j : g[i]) {
if (ch == -1 || (int) st[ch].size() < st[j].size()) {
ch = j;
}
}
if (ch != -1) {
st[i].swap(st[ch]);
}
for (int j : g[i]) {
if (j == ch) {
continue;
}
for (auto& x : st[j]) {
int pos = x.first;
long long val = x.second;
auto it = st[i].lower_bound({pos, -1LL});
if (it != st[i].end() && it->first == pos) {
long long old_v = it->second;
st[i].erase(it);
st[i].emplace(pos, old_v + val);
} else {
st[i].insert(x);
}
}
}
long long ft = 0;
while (true) {
auto it = st[i].lower_bound({h[i], -1LL});
if (it == st[i].begin()) {
break;
}
it = prev(it);
if (it->second + ft <= c[i]) {
ft += it->second;
st[i].erase(it);
} else {
int pos = it->first;
long long val = it->second;
st[i].erase(it);
st[i].emplace(pos, val + ft - c[i]);
break;
}
}
auto it = st[i].lower_bound({h[i], -1LL});
if (it != st[i].end() && it->first == h[i]) {
long long val = it->second;
st[i].erase(it);
st[i].emplace(h[i], val + c[i]);
} else {
st[i].emplace(h[i], c[i]);
}
}
long long sum = accumulate(c.begin(), c.end(), 0LL);
for (auto& p : st[0]) {
sum -= p.second;
}
cout << sum << '\n';
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:44:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | if (ch == -1 || (int) st[ch].size() < st[j].size()) {
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1240 KB |
Output is correct |
8 |
Correct |
5 ms |
1884 KB |
Output is correct |
9 |
Correct |
4 ms |
1400 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
3 ms |
1116 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
2260 KB |
Output is correct |
17 |
Correct |
5 ms |
1880 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
4 ms |
1368 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1624 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
4 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1112 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1524 KB |
Output is correct |
30 |
Correct |
4 ms |
1504 KB |
Output is correct |
31 |
Correct |
4 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1240 KB |
Output is correct |
8 |
Correct |
5 ms |
1884 KB |
Output is correct |
9 |
Correct |
4 ms |
1400 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
3 ms |
1116 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
2260 KB |
Output is correct |
17 |
Correct |
5 ms |
1880 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
4 ms |
1368 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1624 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
4 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1112 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1524 KB |
Output is correct |
30 |
Correct |
4 ms |
1504 KB |
Output is correct |
31 |
Correct |
4 ms |
1628 KB |
Output is correct |
32 |
Correct |
5 ms |
1884 KB |
Output is correct |
33 |
Correct |
294 ms |
80220 KB |
Output is correct |
34 |
Correct |
231 ms |
55488 KB |
Output is correct |
35 |
Correct |
281 ms |
77252 KB |
Output is correct |
36 |
Correct |
229 ms |
55644 KB |
Output is correct |
37 |
Correct |
135 ms |
37060 KB |
Output is correct |
38 |
Correct |
100 ms |
32712 KB |
Output is correct |
39 |
Correct |
132 ms |
29896 KB |
Output is correct |
40 |
Correct |
127 ms |
29812 KB |
Output is correct |
41 |
Correct |
75 ms |
29896 KB |
Output is correct |
42 |
Correct |
138 ms |
29892 KB |
Output is correct |
43 |
Correct |
127 ms |
29892 KB |
Output is correct |
44 |
Correct |
293 ms |
105996 KB |
Output is correct |
45 |
Correct |
204 ms |
70344 KB |
Output is correct |
46 |
Correct |
70 ms |
32712 KB |
Output is correct |
47 |
Correct |
171 ms |
40000 KB |
Output is correct |
48 |
Correct |
110 ms |
32732 KB |
Output is correct |
49 |
Correct |
75 ms |
32732 KB |
Output is correct |
50 |
Correct |
148 ms |
48364 KB |
Output is correct |
51 |
Correct |
92 ms |
36072 KB |
Output is correct |
52 |
Correct |
169 ms |
40520 KB |
Output is correct |
53 |
Correct |
108 ms |
33376 KB |
Output is correct |
54 |
Correct |
65 ms |
30172 KB |
Output is correct |
55 |
Correct |
131 ms |
41420 KB |
Output is correct |
56 |
Correct |
115 ms |
40172 KB |
Output is correct |
57 |
Correct |
116 ms |
40400 KB |
Output is correct |
58 |
Correct |
217 ms |
47856 KB |
Output is correct |
59 |
Correct |
202 ms |
47844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1240 KB |
Output is correct |
8 |
Correct |
5 ms |
1884 KB |
Output is correct |
9 |
Correct |
4 ms |
1400 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
3 ms |
1116 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
2260 KB |
Output is correct |
17 |
Correct |
5 ms |
1880 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
4 ms |
1368 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1624 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
4 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1112 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1524 KB |
Output is correct |
30 |
Correct |
4 ms |
1504 KB |
Output is correct |
31 |
Correct |
4 ms |
1628 KB |
Output is correct |
32 |
Correct |
5 ms |
1884 KB |
Output is correct |
33 |
Correct |
294 ms |
80220 KB |
Output is correct |
34 |
Correct |
231 ms |
55488 KB |
Output is correct |
35 |
Correct |
281 ms |
77252 KB |
Output is correct |
36 |
Correct |
229 ms |
55644 KB |
Output is correct |
37 |
Correct |
135 ms |
37060 KB |
Output is correct |
38 |
Correct |
100 ms |
32712 KB |
Output is correct |
39 |
Correct |
132 ms |
29896 KB |
Output is correct |
40 |
Correct |
127 ms |
29812 KB |
Output is correct |
41 |
Correct |
75 ms |
29896 KB |
Output is correct |
42 |
Correct |
138 ms |
29892 KB |
Output is correct |
43 |
Correct |
127 ms |
29892 KB |
Output is correct |
44 |
Correct |
293 ms |
105996 KB |
Output is correct |
45 |
Correct |
204 ms |
70344 KB |
Output is correct |
46 |
Correct |
70 ms |
32712 KB |
Output is correct |
47 |
Correct |
171 ms |
40000 KB |
Output is correct |
48 |
Correct |
110 ms |
32732 KB |
Output is correct |
49 |
Correct |
75 ms |
32732 KB |
Output is correct |
50 |
Correct |
148 ms |
48364 KB |
Output is correct |
51 |
Correct |
92 ms |
36072 KB |
Output is correct |
52 |
Correct |
169 ms |
40520 KB |
Output is correct |
53 |
Correct |
108 ms |
33376 KB |
Output is correct |
54 |
Correct |
65 ms |
30172 KB |
Output is correct |
55 |
Correct |
131 ms |
41420 KB |
Output is correct |
56 |
Correct |
115 ms |
40172 KB |
Output is correct |
57 |
Correct |
116 ms |
40400 KB |
Output is correct |
58 |
Correct |
217 ms |
47856 KB |
Output is correct |
59 |
Correct |
202 ms |
47844 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |