#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++) {
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);
}
}
vector<set<pair<int, long long>>> st(n);
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]);
}
}
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]);
}
}
vector<bool> was(n);
for (int i : que) {
was[i] = true;
}
long long sum = accumulate(c.begin(), c.end(), 0LL);
for (int ver = 0; ver < n; ver++) {
if (was[ver]) {
continue;
}
vector<int> cyc;
int x = ver;
while (!was[x]) {
was[x] = true;
cyc.push_back(x);
x = a[x];
}
vector<int> ch;
for (int i : cyc) {
for (int j : g[i]) {
if (deg[j] == 0) {
ch.push_back(j);
}
}
}
sort(ch.begin(), ch.end());
ch.erase(unique(ch.begin(), ch.end()), ch.end());
vector<pair<int, long long>> dp;
for (int i : ch) {
assert(!st[i].empty());
for (auto& p : st[i]) {
dp.push_back(p);
}
}
sort(dp.rbegin(), dp.rend());
map<int, long long> mp;
vector<int> f(1, -1);
for (int i : cyc) {
mp[h[i]] += c[i];
f.push_back(h[i]);
}
sort(f.rbegin(), f.rend());
f.erase(unique(f.begin(), f.end()), f.end());
int ptr = 0;
long long ft = 0, mx = 0;
for (int v : f) {
while (ptr < (int) dp.size() && dp[ptr].first >= v) {
ft += dp[ptr].second;
ptr += 1;
}
mx = max(mx, ft + mp[v]);
}
sum -= mx;
}
cout << sum << '\n';
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:41: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]
41 | if (ch == -1 || (int) st[ch].size() < st[j].size()) {
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1444 KB |
Output is correct |
7 |
Correct |
3 ms |
1116 KB |
Output is correct |
8 |
Correct |
5 ms |
1884 KB |
Output is correct |
9 |
Correct |
4 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
2140 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1112 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
4 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1116 KB |
Output is correct |
26 |
Correct |
2 ms |
1132 KB |
Output is correct |
27 |
Correct |
3 ms |
1464 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1372 KB |
Output is correct |
30 |
Correct |
3 ms |
1372 KB |
Output is correct |
31 |
Correct |
3 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1444 KB |
Output is correct |
7 |
Correct |
3 ms |
1116 KB |
Output is correct |
8 |
Correct |
5 ms |
1884 KB |
Output is correct |
9 |
Correct |
4 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
2140 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1112 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
4 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1116 KB |
Output is correct |
26 |
Correct |
2 ms |
1132 KB |
Output is correct |
27 |
Correct |
3 ms |
1464 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1372 KB |
Output is correct |
30 |
Correct |
3 ms |
1372 KB |
Output is correct |
31 |
Correct |
3 ms |
1368 KB |
Output is correct |
32 |
Correct |
5 ms |
1884 KB |
Output is correct |
33 |
Correct |
271 ms |
75080 KB |
Output is correct |
34 |
Correct |
214 ms |
50668 KB |
Output is correct |
35 |
Correct |
290 ms |
72472 KB |
Output is correct |
36 |
Correct |
228 ms |
50640 KB |
Output is correct |
37 |
Correct |
136 ms |
32200 KB |
Output is correct |
38 |
Correct |
114 ms |
27748 KB |
Output is correct |
39 |
Correct |
130 ms |
24936 KB |
Output is correct |
40 |
Correct |
122 ms |
25032 KB |
Output is correct |
41 |
Correct |
77 ms |
25048 KB |
Output is correct |
42 |
Correct |
131 ms |
25032 KB |
Output is correct |
43 |
Correct |
125 ms |
25036 KB |
Output is correct |
44 |
Correct |
295 ms |
104024 KB |
Output is correct |
45 |
Correct |
197 ms |
65392 KB |
Output is correct |
46 |
Correct |
67 ms |
27848 KB |
Output is correct |
47 |
Correct |
168 ms |
36840 KB |
Output is correct |
48 |
Correct |
112 ms |
27848 KB |
Output is correct |
49 |
Correct |
74 ms |
27764 KB |
Output is correct |
50 |
Correct |
81 ms |
36692 KB |
Output is correct |
51 |
Correct |
81 ms |
38744 KB |
Output is correct |
52 |
Correct |
169 ms |
37584 KB |
Output is correct |
53 |
Correct |
108 ms |
28320 KB |
Output is correct |
54 |
Correct |
67 ms |
25032 KB |
Output is correct |
55 |
Correct |
137 ms |
42056 KB |
Output is correct |
56 |
Correct |
121 ms |
40648 KB |
Output is correct |
57 |
Correct |
116 ms |
42204 KB |
Output is correct |
58 |
Correct |
183 ms |
42816 KB |
Output is correct |
59 |
Correct |
185 ms |
41456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1444 KB |
Output is correct |
7 |
Correct |
3 ms |
1116 KB |
Output is correct |
8 |
Correct |
5 ms |
1884 KB |
Output is correct |
9 |
Correct |
4 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
2140 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1112 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
4 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1116 KB |
Output is correct |
26 |
Correct |
2 ms |
1132 KB |
Output is correct |
27 |
Correct |
3 ms |
1464 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1372 KB |
Output is correct |
30 |
Correct |
3 ms |
1372 KB |
Output is correct |
31 |
Correct |
3 ms |
1368 KB |
Output is correct |
32 |
Correct |
5 ms |
1884 KB |
Output is correct |
33 |
Correct |
271 ms |
75080 KB |
Output is correct |
34 |
Correct |
214 ms |
50668 KB |
Output is correct |
35 |
Correct |
290 ms |
72472 KB |
Output is correct |
36 |
Correct |
228 ms |
50640 KB |
Output is correct |
37 |
Correct |
136 ms |
32200 KB |
Output is correct |
38 |
Correct |
114 ms |
27748 KB |
Output is correct |
39 |
Correct |
130 ms |
24936 KB |
Output is correct |
40 |
Correct |
122 ms |
25032 KB |
Output is correct |
41 |
Correct |
77 ms |
25048 KB |
Output is correct |
42 |
Correct |
131 ms |
25032 KB |
Output is correct |
43 |
Correct |
125 ms |
25036 KB |
Output is correct |
44 |
Correct |
295 ms |
104024 KB |
Output is correct |
45 |
Correct |
197 ms |
65392 KB |
Output is correct |
46 |
Correct |
67 ms |
27848 KB |
Output is correct |
47 |
Correct |
168 ms |
36840 KB |
Output is correct |
48 |
Correct |
112 ms |
27848 KB |
Output is correct |
49 |
Correct |
74 ms |
27764 KB |
Output is correct |
50 |
Correct |
81 ms |
36692 KB |
Output is correct |
51 |
Correct |
81 ms |
38744 KB |
Output is correct |
52 |
Correct |
169 ms |
37584 KB |
Output is correct |
53 |
Correct |
108 ms |
28320 KB |
Output is correct |
54 |
Correct |
67 ms |
25032 KB |
Output is correct |
55 |
Correct |
137 ms |
42056 KB |
Output is correct |
56 |
Correct |
121 ms |
40648 KB |
Output is correct |
57 |
Correct |
116 ms |
42204 KB |
Output is correct |
58 |
Correct |
183 ms |
42816 KB |
Output is correct |
59 |
Correct |
185 ms |
41456 KB |
Output is correct |
60 |
Correct |
1 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 |
348 KB |
Output is correct |
64 |
Correct |
271 ms |
61412 KB |
Output is correct |
65 |
Correct |
208 ms |
43648 KB |
Output is correct |
66 |
Correct |
260 ms |
63676 KB |
Output is correct |
67 |
Correct |
218 ms |
48584 KB |
Output is correct |
68 |
Correct |
140 ms |
35188 KB |
Output is correct |
69 |
Correct |
115 ms |
32204 KB |
Output is correct |
70 |
Correct |
158 ms |
38804 KB |
Output is correct |
71 |
Correct |
97 ms |
30520 KB |
Output is correct |
72 |
Correct |
172 ms |
43192 KB |
Output is correct |
73 |
Correct |
96 ms |
31356 KB |
Output is correct |
74 |
Correct |
130 ms |
41440 KB |
Output is correct |
75 |
Correct |
104 ms |
35908 KB |
Output is correct |
76 |
Correct |
98 ms |
35908 KB |
Output is correct |
77 |
Correct |
123 ms |
42036 KB |
Output is correct |
78 |
Correct |
98 ms |
36056 KB |
Output is correct |
79 |
Correct |
204 ms |
58832 KB |
Output is correct |
80 |
Correct |
174 ms |
43752 KB |
Output is correct |
81 |
Correct |
109 ms |
34384 KB |
Output is correct |
82 |
Correct |
128 ms |
43176 KB |
Output is correct |
83 |
Correct |
85 ms |
29108 KB |
Output is correct |
84 |
Correct |
202 ms |
45660 KB |
Output is correct |
85 |
Correct |
194 ms |
45252 KB |
Output is correct |
86 |
Correct |
181 ms |
42176 KB |
Output is correct |
87 |
Correct |
182 ms |
45468 KB |
Output is correct |
88 |
Correct |
185 ms |
45404 KB |
Output is correct |