This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, idx, totC, totR, A[200005], H[200005], C[200005], link[200005], sz[200005];
bool on_cyc[200005];
vector<int> adj[200005];
set<iii> S[200005];
int find(int x) {
if (x == link[x]) return x;
return link[x] = find(link[x]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
if (sz[b] > sz[a]) swap(a, b);
sz[a] += sz[b];
link[b] = a;
}
void dp(int n, int e = -1) {
for (auto u : adj[n]) if (u != e) {
dp(u, n);
if (S[u].size() > S[n].size()) swap(S[n], S[u]);
for (auto i : S[u]) S[n].insert(i);
}
vector<set<iii>::iterator> to_del;
auto it = S[n].upper_bound(mt(H[n], LLONG_MAX, LLONG_MAX));
for (int bal = C[n]; bal > 0 && it != S[n].end(); it++) {
auto [h, c, tmp] = *it;
if (bal > c) {
to_del.pb(it);
bal -= c;
} else if (bal == c) {
to_del.pb(it);
bal = 0;
} else {
to_del.pb(it);
S[n].insert(mt(h, c - bal, ++idx));
bal = 0;
}
}
S[n].insert(mt(H[n], C[n], ++idx));
for (auto i : to_del) S[n].erase(i);
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
link[i] = i;
sz[i] = 1;
}
for (int i = 1; i <= N; i++) {
cin >> A[i] >> H[i] >> C[i];
H[i] = -H[i];
adj[A[i]].pb(i);
unite(i, A[i]);
totC += C[i];
}
for (int i = 1; i <= N; i++)
if (i == find(i)) {
set<iii> SS;
map<int, int> mpp;
int tort = i, hare = i;
do {
tort = A[tort];
hare = A[A[hare]];
} while (tort != hare);
do {
on_cyc[tort] = 1;
tort = A[tort];
} while (tort != hare);
do {
for (auto u : adj[tort])
if (!on_cyc[u]) {
dp(u);
if (SS.size() < S[u].size()) swap(SS, S[u]);
for (auto v : S[u]) SS.insert(v);
}
tort = A[tort];
mpp[H[tort]] += C[tort];
} while (tort != hare);
auto it = SS.begin();
int sf = 0, cans = 0;
for (auto [a, b, c] : SS) cans += b;
for (auto j : mpp) {
while (it != SS.end() && g0(*it) <= j.first) sf += g1(*it), ++it;
cans = max(cans, sf + j.second);
}
totR += cans;
}
cout << totC - totR << '\n';
}
Compilation message (stderr)
worst_reporter2.cpp:74:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
74 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |