#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);
}
}
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]);
}
}
long long sum = accumulate(c.begin(), c.end(), 0LL);
for (int ver = 0; ver < n; ver++) {
if (deg[ver] == 0) {
continue;
}
vector<int> cyc;
int x = ver;
while (true) {
cyc.push_back(x);
x = a[x];
if (x == ver) {
break;
}
}
vector<int> ch;
for (int i : cyc) {
for (int j : g[i]) {
if (deg[j] == 0) {
ch.push_back(j);
}
}
}
vector<pair<int, long long>> dp;
for (int i : ch) {
for (auto& p : st[i]) {
dp.push_back(p);
}
}
sort(dp.rbegin(), dp.rbegin());
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());
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;
for (int i : cyc) {
deg[i] = 0;
}
}
cout << sum << '\n';
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:43: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]
43 | if (ch == -1 || (int) st[ch].size() < st[j].size()) {
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
1912 KB |
Output is correct |
6 |
Correct |
4 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1372 KB |
Output is correct |
8 |
Correct |
5 ms |
2140 KB |
Output is correct |
9 |
Correct |
4 ms |
1628 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1148 KB |
Output is correct |
12 |
Correct |
3 ms |
1116 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
4 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1268 KB |
Output is correct |
16 |
Correct |
6 ms |
2376 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
4 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1140 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1392 KB |
Output is correct |
24 |
Correct |
4 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1144 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1648 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1372 KB |
Output is correct |
30 |
Correct |
5 ms |
1624 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 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 |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
1912 KB |
Output is correct |
6 |
Correct |
4 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1372 KB |
Output is correct |
8 |
Correct |
5 ms |
2140 KB |
Output is correct |
9 |
Correct |
4 ms |
1628 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1148 KB |
Output is correct |
12 |
Correct |
3 ms |
1116 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
4 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1268 KB |
Output is correct |
16 |
Correct |
6 ms |
2376 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
4 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1140 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1392 KB |
Output is correct |
24 |
Correct |
4 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1144 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1648 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1372 KB |
Output is correct |
30 |
Correct |
5 ms |
1624 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
5 ms |
2140 KB |
Output is correct |
33 |
Correct |
273 ms |
78420 KB |
Output is correct |
34 |
Correct |
211 ms |
53624 KB |
Output is correct |
35 |
Correct |
255 ms |
76376 KB |
Output is correct |
36 |
Correct |
221 ms |
53448 KB |
Output is correct |
37 |
Correct |
137 ms |
34920 KB |
Output is correct |
38 |
Correct |
101 ms |
30404 KB |
Output is correct |
39 |
Correct |
149 ms |
27852 KB |
Output is correct |
40 |
Correct |
125 ms |
27676 KB |
Output is correct |
41 |
Correct |
74 ms |
27852 KB |
Output is correct |
42 |
Correct |
148 ms |
27852 KB |
Output is correct |
43 |
Correct |
131 ms |
27752 KB |
Output is correct |
44 |
Correct |
300 ms |
105836 KB |
Output is correct |
45 |
Correct |
200 ms |
68292 KB |
Output is correct |
46 |
Correct |
70 ms |
30552 KB |
Output is correct |
47 |
Correct |
183 ms |
39996 KB |
Output is correct |
48 |
Correct |
111 ms |
30660 KB |
Output is correct |
49 |
Correct |
75 ms |
30664 KB |
Output is correct |
50 |
Correct |
61 ms |
39228 KB |
Output is correct |
51 |
Correct |
68 ms |
39680 KB |
Output is correct |
52 |
Correct |
167 ms |
40364 KB |
Output is correct |
53 |
Correct |
107 ms |
31048 KB |
Output is correct |
54 |
Correct |
66 ms |
27752 KB |
Output is correct |
55 |
Correct |
137 ms |
45816 KB |
Output is correct |
56 |
Correct |
120 ms |
44356 KB |
Output is correct |
57 |
Correct |
124 ms |
42828 KB |
Output is correct |
58 |
Correct |
189 ms |
45152 KB |
Output is correct |
59 |
Correct |
178 ms |
44752 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 |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
1912 KB |
Output is correct |
6 |
Correct |
4 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1372 KB |
Output is correct |
8 |
Correct |
5 ms |
2140 KB |
Output is correct |
9 |
Correct |
4 ms |
1628 KB |
Output is correct |
10 |
Correct |
3 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1148 KB |
Output is correct |
12 |
Correct |
3 ms |
1116 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
4 ms |
1116 KB |
Output is correct |
15 |
Correct |
3 ms |
1268 KB |
Output is correct |
16 |
Correct |
6 ms |
2376 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
4 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1368 KB |
Output is correct |
21 |
Correct |
2 ms |
1140 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1392 KB |
Output is correct |
24 |
Correct |
4 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1144 KB |
Output is correct |
26 |
Correct |
2 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1648 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1372 KB |
Output is correct |
30 |
Correct |
5 ms |
1624 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
5 ms |
2140 KB |
Output is correct |
33 |
Correct |
273 ms |
78420 KB |
Output is correct |
34 |
Correct |
211 ms |
53624 KB |
Output is correct |
35 |
Correct |
255 ms |
76376 KB |
Output is correct |
36 |
Correct |
221 ms |
53448 KB |
Output is correct |
37 |
Correct |
137 ms |
34920 KB |
Output is correct |
38 |
Correct |
101 ms |
30404 KB |
Output is correct |
39 |
Correct |
149 ms |
27852 KB |
Output is correct |
40 |
Correct |
125 ms |
27676 KB |
Output is correct |
41 |
Correct |
74 ms |
27852 KB |
Output is correct |
42 |
Correct |
148 ms |
27852 KB |
Output is correct |
43 |
Correct |
131 ms |
27752 KB |
Output is correct |
44 |
Correct |
300 ms |
105836 KB |
Output is correct |
45 |
Correct |
200 ms |
68292 KB |
Output is correct |
46 |
Correct |
70 ms |
30552 KB |
Output is correct |
47 |
Correct |
183 ms |
39996 KB |
Output is correct |
48 |
Correct |
111 ms |
30660 KB |
Output is correct |
49 |
Correct |
75 ms |
30664 KB |
Output is correct |
50 |
Correct |
61 ms |
39228 KB |
Output is correct |
51 |
Correct |
68 ms |
39680 KB |
Output is correct |
52 |
Correct |
167 ms |
40364 KB |
Output is correct |
53 |
Correct |
107 ms |
31048 KB |
Output is correct |
54 |
Correct |
66 ms |
27752 KB |
Output is correct |
55 |
Correct |
137 ms |
45816 KB |
Output is correct |
56 |
Correct |
120 ms |
44356 KB |
Output is correct |
57 |
Correct |
124 ms |
42828 KB |
Output is correct |
58 |
Correct |
189 ms |
45152 KB |
Output is correct |
59 |
Correct |
178 ms |
44752 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
344 KB |
Output is correct |
64 |
Correct |
264 ms |
66680 KB |
Output is correct |
65 |
Incorrect |
234 ms |
48656 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |