#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;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false
const int MAXN = 200 * 1000 + 17;
const int INF = 1000 * 1000 * 1000;
int a[MAXN];
int h[MAXN];
ll c[MAXN];
vector<int> graf[MAXN];
map<int, ll>* DFS (int v) {
map<int, ll>* dp1 = new map<int, ll>();
for (auto x : graf[v]) {
map<int, ll>* dp2 = DFS(x);
if (int(dp1->size()) < int(dp2->size())) {
swap(dp1, dp2);
}
for (auto y : *dp2) {
(*dp1)[y.st] += y.nd;
}
}
(*dp1)[h[v]] += c[v];
ll dod = c[v];
while (!dp1->empty() && dod > 0) {
auto it = dp1->begin();
if ((*it).st >= h[v]) {
break;
}
ll x = min(dod, it->nd);
dod -= x;
it->nd -= x;
if (it->nd == 0) {
dp1->erase(it);
}
}
return dp1;
}
int main () {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> a[i] >> h[i] >> c[i];
}
for (int i = 2; i <= n; i ++) {
graf[a[i]].pb(i);
}
ll wyn = 0;
map<int, ll>* dp = DFS(1);
for (int i = 1; i <= n; i ++) {
wyn += c[i];
}
for (auto x : *dp) {
wyn -= x.nd;
}
cout << wyn << "\n";
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... |