//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define FORR(i, a, b) for(int i = a; i >= b; --i)
#define pii pair<int, int>
#define ff first
#define ss second
#define pll pair<ll, ll>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vpl vector<pll>
#define vvpl vector<vpl>
#define vl vector<ll>
#define vvl vector<vl>
//#define x first
//#define y second
using namespace std;
const int N = 1e5 + 9, LGV = 33, LGN = 21, Mod = 1e9 + 7;
//const int Inf = 0x3f3f3f3f;
const ll Inf = LLONG_MAX / 2;
const bool test_case = false;
//ifstream fin("landscape.in");
//ofstream fout("landscape.out");
//#define cin fin
//#define cout fout
int n, m, k, d[N], f[N];
multiset<pll> mst[N];
vvi G(N);
void Dfs(int x, int p)
{
for(auto y : G[x])
{
Dfs(y, x);
if(mst[x].size() < mst[y].size())
swap(mst[x], mst[y]);
for(auto e : mst[y])mst[x].insert(e);
}
if(d[x])
{
multiset<pll>::iterator it = mst[x].insert({d[x], f[x]});
ll v = f[x];
auto nx = it; ++nx;
while(nx != mst[x].end())
if(v >= (*nx).ss)
{
v -= (*nx).ss;
nx = mst[x].erase(nx);
}
else
{
auto aux = *nx;
mst[x].erase(nx);
aux.ss -= v;
mst[x].insert(aux);
break;
}
}
}
void solve()
{
cin >> n >> m >> k;
int p;
FOR(i, 2, n)
{
cin >> p;
G[p].pb(i);
}
int v;
FOR(i, 1, m)
cin >> v >> d[v] >> f[v];
Dfs(1, 0);
ll ans = 0;
for(auto [k, v] : mst[1])
ans += v;
cout << ans << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t = 1;
if(test_case)cin >> t;
while(t --)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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |