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 ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=200005;
ll H[maxn], C[maxn];
map <ll, ll> cnt[maxn];
vector <ll> A[maxn];
void dfs(ll u)
{
for (ll v:A[u])
{
dfs(v);
if (cnt[u].size()<cnt[v].size())
swap(cnt[u], cnt[v]);
for (pll i:cnt[v])
cnt[u][i.fi]+=i.se;
}
ll crr=C[u]; cnt[u][H[u]]+=crr;
auto it=cnt[u].lower_bound(H[u]);
while (it!=cnt[u].begin())
{
it--;
if ((*it).se>crr) {(*it).se-=crr;break;}
crr-=(*it).se; it=cnt[u].erase(it);
}
// cout << u << "\n";
// for (pll i:cnt[u]) cout << i.fi << " " << i.se << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, sum=0; cin >> n;
for (ll i=1; i<=n; i++)
{
ll a; cin >> a;
if (i!=1) A[a].pb(i);
cin >> H[i] >> C[i]; sum+=C[i];
}
dfs(1);
for (pll i:cnt[1]) sum-=i.se;
cout << sum << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |