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>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const int szBL = 320;
const int INF = 1e9;
const int BASE = 137;
struct Data {
int A, H, C;
};
int n;
Data a[N];
int szC[N];
vector<int> ke[N];
map<int,ll> dp[N];///dp[u]: decreasing function
int dd[N];
void dfs (int u) {
dd[u] = 1;
szC[u] = 1;
int mxV = -1;
// cout << u <<"\n";
iter (&v, ke[u]) {
dfs (v);
szC[u] += szC[v];
if (mxV == -1 || szC[v] > szC[mxV]) {
mxV = v;
}
}
if (mxV != -1) swap(dp[u], dp[mxV]); dp[u][1] += 0;
iter (&v, ke[u]) {
if (v == mxV) continue;
iter (&id, dp[v]) {
if (id.fs != 1) {
dp[u][id.fs] += id.se;
dp[u][1] -= id.se;
// cout << "upd: "<<u<<" "<<v<<" "<<id.fs<<" "<<id.se <<"\n";
}
}
map<int,ll> ().swap(dp[v]);
}
dp[u][a[u].H + 1] -= a[u].C;
ll val = a[u].C;
auto it = dp[u].lower_bound(a[u].H + 1);
// cout << a[u].H<<"\n";
// cout << u<<" "<<dp[u][1]<<" aa\n";
while (1) {
--it;
// cout << it->fs<<" "<<it->se<<"\n";
if (it != dp[u].begin() && val >= -it->se) {
val += it->se;
it = dp[u].erase(it);
}
else {
it->se += val;
val = 0;
break;
}
}
// dp[u][1] += val;
// cout << u <<" "<<dp[u][1] <<" "<<val<<"\n";
}
vector<int> zip;
void compress () {
rep (i, 1, n) zip.push_back(a[i].H);
sort (ALL(zip));
zip.resize(unique(ALL(zip)) - zip.begin());
rep (i, 1, n) {
a[i].H = lower_bound(ALL(zip), a[i].H) - zip.begin() + 1;
}
}
void solution () {
cin >> n;
ll tot = 0;
rep (i, 1, n) {
cin >> a[i].A >> a[i].H >> a[i].C;
tot += a[i].C;
if (a[i].A != i)
ke[a[i].A].push_back(i);
}
compress();
rep (i, 1, n) {
if (a[i].A != i) continue;
dfs(i);
cout << tot - dp[i][1] <<"\n";
}
}
#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);
int main () {
// file("c");
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
no bug challenge
*/
Compilation message (stderr)
worst_reporter2.cpp: In function 'void dfs(int)':
worst_reporter2.cpp:49:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
49 | if (mxV != -1) swap(dp[u], dp[mxV]); dp[u][1] += 0;
| ^~
worst_reporter2.cpp:49:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
49 | if (mxV != -1) swap(dp[u], dp[mxV]); dp[u][1] += 0;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |