#include <bits/stdc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) qqbx(#a, a)
#define pary(a...) danb(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
std::cerr << "\033[1;32m[ " << s << " ] = [ ";
for (auto it = L; it != R; ++it)
std::cerr << *it << ' ';
std::cerr << "]\033[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
using namespace std;
using ll = int64_t;
const int maxn = 200025, maxm = 10000;
int H[maxn], C[maxn];
vector<int> g[maxn];
map<int,ll,greater<>> sub[maxn];
int cyc[maxn];
void join(int a, int b) {
if (sub[a].size() > sub[b].size())
swap(sub[a], sub[b]);
for (auto [h, val]: sub[a])
sub[b][h] += val;
}
void dfs(int i) {
for (int j: g[i]) {
dfs(j);
join(j, i);
}
sub[i][H[i]] += C[i];
auto it = sub[i].upper_bound(H[i]);
ll v = C[i];
while (it != sub[i].end()) {
if (it->second < v) {
v -= it->second;
sub[i].erase(it++);
} else {
it->second -= v;
break;
}
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
ll tot = 0;
vector<int> A(n);
vector<int> indeg(n);
for (int i = 0; i < n; i++) {
cin >> A[i] >> H[i] >> C[i];
tot += C[i];
--A[i];
++indeg[A[i]];
g[A[i]].push_back(i);
}
for (int i = 0; i < n; i++)
for (int i = 0; i < n; i++) cyc[i] = -1;
queue<int> que;
for (int i = 0; i < n; i++) if (indeg[i] == 0) que.push(i), cyc[i] = 0;
while (!que.empty()) {
int i = que.front(); que.pop();
int j = A[i];
if (--indeg[j] == 0)
que.push(j), cyc[j] = 0;
}
vector<vector<int>> roots;
roots.reserve(n);
for (int i = 0, tot = 0; i < n; i++) if (cyc[i] == -1) {
roots.emplace_back();
roots.back().emplace_back(i);
cyc[i] = ++tot;
for (int x = A[i]; x != i; x = A[x]) {
roots.back().emplace_back(x);
cyc[x] = cyc[i];
}
}
ll ans = 0;
for (auto R: roots) {
map<int,ll> best;
for (int x: R) {
ll dp = C[x];
for (int y: g[x]) {
if (cyc[y]) continue;
dfs(y);
for (auto [h, val]: sub[y]) if (h >= H[x]) dp += val;
join(y, R[0]);
}
best[H[x]] += dp;
}
ll mx = 0;
for (auto [h, val]: sub[R[0]]) mx += val;
for (auto [h, val]: best) mx = max(mx, val);
ans += mx;
}
cout << tot - ans << '\n';
}
Compilation message
worst_reporter2.cpp: In function 'void join(int, int)':
worst_reporter2.cpp:34:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for (auto [h, val]: sub[a])
| ^
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:98:27: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
98 | for (auto [h, val]: sub[y]) if (h >= H[x]) dp += val;
| ^
worst_reporter2.cpp:104:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
104 | for (auto [h, val]: sub[R[0]]) mx += val;
| ^
worst_reporter2.cpp:105:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
105 | for (auto [h, val]: best) mx = max(mx, val);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14388 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14340 KB |
Output is correct |
4 |
Correct |
9 ms |
14296 KB |
Output is correct |
5 |
Correct |
19 ms |
15564 KB |
Output is correct |
6 |
Correct |
21 ms |
15052 KB |
Output is correct |
7 |
Correct |
17 ms |
14852 KB |
Output is correct |
8 |
Correct |
19 ms |
15616 KB |
Output is correct |
9 |
Correct |
17 ms |
15052 KB |
Output is correct |
10 |
Correct |
16 ms |
14924 KB |
Output is correct |
11 |
Correct |
16 ms |
14728 KB |
Output is correct |
12 |
Correct |
16 ms |
15056 KB |
Output is correct |
13 |
Correct |
14 ms |
14976 KB |
Output is correct |
14 |
Correct |
16 ms |
14796 KB |
Output is correct |
15 |
Correct |
17 ms |
14872 KB |
Output is correct |
16 |
Correct |
19 ms |
15960 KB |
Output is correct |
17 |
Correct |
17 ms |
15332 KB |
Output is correct |
18 |
Correct |
15 ms |
14796 KB |
Output is correct |
19 |
Correct |
16 ms |
15052 KB |
Output is correct |
20 |
Correct |
16 ms |
14924 KB |
Output is correct |
21 |
Correct |
16 ms |
14924 KB |
Output is correct |
22 |
Correct |
16 ms |
15096 KB |
Output is correct |
23 |
Correct |
15 ms |
14796 KB |
Output is correct |
24 |
Correct |
16 ms |
15052 KB |
Output is correct |
25 |
Correct |
16 ms |
14924 KB |
Output is correct |
26 |
Correct |
20 ms |
15296 KB |
Output is correct |
27 |
Correct |
17 ms |
15108 KB |
Output is correct |
28 |
Correct |
19 ms |
15180 KB |
Output is correct |
29 |
Correct |
17 ms |
15272 KB |
Output is correct |
30 |
Correct |
16 ms |
15224 KB |
Output is correct |
31 |
Correct |
20 ms |
15308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14388 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14340 KB |
Output is correct |
4 |
Correct |
9 ms |
14296 KB |
Output is correct |
5 |
Correct |
19 ms |
15564 KB |
Output is correct |
6 |
Correct |
21 ms |
15052 KB |
Output is correct |
7 |
Correct |
17 ms |
14852 KB |
Output is correct |
8 |
Correct |
19 ms |
15616 KB |
Output is correct |
9 |
Correct |
17 ms |
15052 KB |
Output is correct |
10 |
Correct |
16 ms |
14924 KB |
Output is correct |
11 |
Correct |
16 ms |
14728 KB |
Output is correct |
12 |
Correct |
16 ms |
15056 KB |
Output is correct |
13 |
Correct |
14 ms |
14976 KB |
Output is correct |
14 |
Correct |
16 ms |
14796 KB |
Output is correct |
15 |
Correct |
17 ms |
14872 KB |
Output is correct |
16 |
Correct |
19 ms |
15960 KB |
Output is correct |
17 |
Correct |
17 ms |
15332 KB |
Output is correct |
18 |
Correct |
15 ms |
14796 KB |
Output is correct |
19 |
Correct |
16 ms |
15052 KB |
Output is correct |
20 |
Correct |
16 ms |
14924 KB |
Output is correct |
21 |
Correct |
16 ms |
14924 KB |
Output is correct |
22 |
Correct |
16 ms |
15096 KB |
Output is correct |
23 |
Correct |
15 ms |
14796 KB |
Output is correct |
24 |
Correct |
16 ms |
15052 KB |
Output is correct |
25 |
Correct |
16 ms |
14924 KB |
Output is correct |
26 |
Correct |
20 ms |
15296 KB |
Output is correct |
27 |
Correct |
17 ms |
15108 KB |
Output is correct |
28 |
Correct |
19 ms |
15180 KB |
Output is correct |
29 |
Correct |
17 ms |
15272 KB |
Output is correct |
30 |
Correct |
16 ms |
15224 KB |
Output is correct |
31 |
Correct |
20 ms |
15308 KB |
Output is correct |
32 |
Correct |
19 ms |
15564 KB |
Output is correct |
33 |
Execution timed out |
2082 ms |
21496 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14388 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14340 KB |
Output is correct |
4 |
Correct |
9 ms |
14296 KB |
Output is correct |
5 |
Correct |
19 ms |
15564 KB |
Output is correct |
6 |
Correct |
21 ms |
15052 KB |
Output is correct |
7 |
Correct |
17 ms |
14852 KB |
Output is correct |
8 |
Correct |
19 ms |
15616 KB |
Output is correct |
9 |
Correct |
17 ms |
15052 KB |
Output is correct |
10 |
Correct |
16 ms |
14924 KB |
Output is correct |
11 |
Correct |
16 ms |
14728 KB |
Output is correct |
12 |
Correct |
16 ms |
15056 KB |
Output is correct |
13 |
Correct |
14 ms |
14976 KB |
Output is correct |
14 |
Correct |
16 ms |
14796 KB |
Output is correct |
15 |
Correct |
17 ms |
14872 KB |
Output is correct |
16 |
Correct |
19 ms |
15960 KB |
Output is correct |
17 |
Correct |
17 ms |
15332 KB |
Output is correct |
18 |
Correct |
15 ms |
14796 KB |
Output is correct |
19 |
Correct |
16 ms |
15052 KB |
Output is correct |
20 |
Correct |
16 ms |
14924 KB |
Output is correct |
21 |
Correct |
16 ms |
14924 KB |
Output is correct |
22 |
Correct |
16 ms |
15096 KB |
Output is correct |
23 |
Correct |
15 ms |
14796 KB |
Output is correct |
24 |
Correct |
16 ms |
15052 KB |
Output is correct |
25 |
Correct |
16 ms |
14924 KB |
Output is correct |
26 |
Correct |
20 ms |
15296 KB |
Output is correct |
27 |
Correct |
17 ms |
15108 KB |
Output is correct |
28 |
Correct |
19 ms |
15180 KB |
Output is correct |
29 |
Correct |
17 ms |
15272 KB |
Output is correct |
30 |
Correct |
16 ms |
15224 KB |
Output is correct |
31 |
Correct |
20 ms |
15308 KB |
Output is correct |
32 |
Correct |
19 ms |
15564 KB |
Output is correct |
33 |
Execution timed out |
2082 ms |
21496 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |