#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)size(x)
#define pb push_back
#define ff first
#define ss second
typedef long long ll;
typedef pair<int, int> pp;
void dbg(){
cerr << endl;
}
template<typename H, typename... T> void dbg(H h, T... t){
cerr << h << ", ";
dbg(t...);
}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">: ", dbg(__VA_ARGS__)
#else
#define er(...) 0
#endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, mod2 = 1e9 + 9, maxn = 2e5 + 5, lg = 21, inf = ll(1e9) + 5;
ll pw(ll a, ll b){
if(b == 0) return 1;
ll k = pw(a, b>>1);
return k*k*(b&1ll?a:1);
}
vector<int> adj[maxn];
ll par[maxn], H[maxn], C[maxn], nxt[maxn];
int h[maxn], cnt[maxn], top[maxn], st[maxn], en[maxn], t, eu[maxn];
ll dp[maxn];
int seg[maxn<<2];
int get(int l, int r, int x = 1, int lx = 0, int rx = t){
if(l <= lx && r >= rx) return seg[x];
int mid = (lx + rx)>>1;
int res = -1;
if(mid < r) res = get(l, r, x<<1|1, mid, rx);
if(res + 1) return res;
if(l < mid) return get(l, r, x<<1, lx, mid);
return -1;
}
void upd(int i, bool act, int x = 1, int lx = 0, int rx = t){
if(lx + 1 == rx){
seg[x] = act? i: -1;
return;
}
int mid = (lx + rx)>>1;
if(i < mid) upd(i, act, x<<1, lx, mid);
else upd(i, act, x<<1|1, mid, rx);
seg[x] = max(seg[x<<1], seg[x<<1|1]);
}
ll fen[maxn];
ll getf(int i){
ll res = 0;
for(i++; i; i -= i&-i) res += fen[i];
return res;
}
ll getf(int l, int r){
return getf(r-1) - getf(l-1);
}
ll getu(int u){
return getf(st[u], en[u]);
}
void updf(int i, ll k){
for(i++; i < maxn; i += i&-i) fen[i] += k;
}
void dfs(int r, int p){
par[r] = p;
cnt[r] = 1;
for(int c: adj[r]){
h[c] = h[r] + 1;
dfs(c, r);
cnt[r] += cnt[c];
}
}
void dfsHLD(int r, int tp){
eu[t] = r;
top[r] = tp, st[r] = t++;
pp bst = {-1, -1};
for(int c: adj[r]) bst = max(bst, {cnt[c], c});
if(bst.ff + 1){
dfsHLD(bst.ss, tp);
for(int c: adj[r]) if(c - bst.ss) dfsHLD(c, c);
}
en[r] = t;
}
void add(int u){
dp[u] = getu(u) + C[u];
ll sm = C[u], cr = u;
upd(st[u], true);
u = par[u];
if(u + 1){
updf(st[u], sm);
}
while(u + 1){
int x = get(st[top[u]], st[u] + 1);
if(x + 1){
x = eu[x];
if(par[x] + 1) updf(st[par[x]], -sm);
ll t = getu(x);
if(t > dp[x]){
sm = t - dp[x];
upd(st[x], false);
u = par[x];
if(u + 1) updf(st[u], sm);
} else break;
} else u = par[top[u]];
}
}
int main(){ IOS();
int n; cin >> n;
ll sum = 0;
rep(i,0,n){
cin >> nxt[i] >> H[i] >> C[i];
nxt[i]--;
sum += C[i];
if(i) assert(nxt[i] < i);
}
rep(i,1,n){
adj[nxt[i]].pb(i);
}
dfs(0, -1);
dfsHLD(0, 0);
fill(seg, seg + (maxn<<2), -1);
vector<int> srt(n);
iota(all(srt), 0);
sort(all(srt), [&](int i, int j){
return pp(H[i], h[i]) > pp(H[j], h[j]);
});
rep(i,0,n){
// er(i, srt[i]);
add(srt[i]);
// er(i);
}
// rep(i,0,n) er(i+1, dp[i], getu(i));
// er(sum, max(getu(0), dp[0]));
cout << sum - max(getu(0), dp[0]) << '\n';
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'void add(int)':
worst_reporter2.cpp:110:20: warning: unused variable 'cr' [-Wunused-variable]
110 | ll sm = C[u], cr = u;
| ^~
worst_reporter2.cpp: In function 'void IOS()':
worst_reporter2.cpp:24:18: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen("inp.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
worst_reporter2.cpp:25:18: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
8276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
8276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
8276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |