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 TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
#define int ll
vector<ll>g[maxN];
map<int,int>mp[maxN];
ll t[maxN],c[maxN];
void dfs(ll u=1)
{
for(int v:g[u])
{
dfs(v);
if(mp[v].size()>mp[u].size()) mp[u].swap(mp[v]);
for(auto zz:mp[v]) mp[u][zz.fi]+=zz.se;
mp[v].clear();
}
if(t[u]>0)
{
ll tru=c[u];
while(tru>0)
{
auto it=mp[u].upper_bound(t[u]);
if(it==mp[u].end()) break;
if(it->se<=tru)
{
tru-=it->se;
mp[u].erase(it);
}
else
{
it->se-=tru;
break;
}
}
mp[u][t[u]]+=c[u];
}
}
ll n,m,k,p[maxN];
void solve()
{
cin >> n >> m >> k;
for(int i=2;i<=n;i++) cin >> p[i],g[p[i]].pb(i);
while(m--)
{
ll u;
cin >> u;
cin >> t[u] >> c[u];
}
ll ans=0;
dfs();
for(auto zz:mp[1]) ans+=zz.se;
cout << ans;
}
main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
magictree.cpp:63:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
63 | main()
| ^~~~
# | 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... |