#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#define int long long
#define fir first
#define sec second
const int maxn=1e5;
int par[maxn+2],day[maxn+2],val[maxn+2];
struct cmp{
bool operator()(const array<int,3>&a,const array<int,3>&b) const{
if(a[1]!=b[1]){
return a[1]<b[1];
}
return a[0]>b[0];
}
};
set<array<int,3>,cmp >apa[maxn+2];
void merge(int a,int b){
if(apa[a].size()>apa[b].size())swap(apa[a],apa[b]);
for(auto x : apa[a]){
apa[b].insert(x);
}
apa[a].clear();
}
signed main(){
int n,m,k;
cin>>n>>m>>k;
for(int q=2;q<=n;q++){
cin>>par[q];
}
for(int q=1;q<=m;q++){
int v,d,w;
cin>>v>>d>>w;
day[v]=d,val[v]=w;
}
for(int q=n;q>1;q--){
if(day[q]){
array<int,3>cur={q,day[q],val[q]};
apa[q].insert(cur);
auto it=apa[q].lower_bound(cur); it++;
while(it!=apa[q].end() && cur[2]>0){
array<int,3>hah=*it;
it++;
apa[q].erase(hah);
cur[2]-=hah[2];
if(cur[2]<0){
hah[2]=-cur[2];
apa[q].insert(hah);
}
}
}
merge(q,par[q]);
}
int ans=0;
for(auto x : apa[1]){
ans+=x[2];
}
cout<<ans<<endl;
}
| # | 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... |