#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
#define int long long
#define sep ' '
#define F first
#define S second
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define kill(x) {cout << x << endl;return;}
#define sz(x) int(x.size())
#define SP(x) setprecision(x)
#define mod(x) (1ll*x%M+M)%M
#define p_q priority_queue
#define REP(i, k) for (int i = 0 ; i < k ; i ++)
#define mid (l+r)/2
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5+10;
int n;
int a[N], r[N], c[N];
vi adj[N];
int sz[N];
map<int, int> mp[N];
int val[N];
void add(int to, int from) {
val[to] += val[from];
for(auto [a, b]: mp[from]) {
mp[to][a] += b;
}
}
void change(int v, int x, int ch) {
int r = ch;
mp[v][x] -= ch;
while(true) {
auto it = mp[v].upper_bound(x);
if (it == mp[v].end()) break;
if ((-(*it).S < r)) {
r -= -(*it).S;
mp[v].erase(it);
} else {
(*it).S -= -r;
break;
}
}
}
void dfs(int v = 0) {
sz[v]++;
int mx = 0, bigch = -1;
for(int neigh: adj[v]) {
dfs(neigh);
if (sz[neigh] > mx) {
mx = sz[neigh], bigch = neigh;
}
sz[v] += sz[neigh];
}
swap(mp[bigch], mp[v]), swap(val[v], val[bigch]);
for(int neigh: adj[v]) {
if (neigh == bigch) continue;
add(v, neigh);
}
val[v] += c[v];
change(v, r[v], c[v]);
}
void solve() {
cin >> n;
for(int i = 0 ; i < n ; i ++) {
cin >> a[i] >> r[i] >> c[i]; a[i] --;
if (i != 0) adj[a[i]].pb(i);
}
// subtask 2
dfs();
int x = val[0];
for(auto [a, b]: mp[0]) {
x += b;
}
cout << x << endl;
}
int32_t main() {
fastIO;
solve();
return 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... |