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>
using namespace std;
#define N 7<<14
#define int long long
vector<int>adj[N];
map<int,int> dp[N];
int d[N],w[N];
void dfs(int n){
dp[n][0];
for(auto i:adj[n]){
dfs(i);
if(size(dp[n])<size(dp[i]))
swap(dp[n],dp[i]);
for(auto&[i,j]:dp[i])
dp[n][i]+=j;
dp[i].clear();
}
if(w[n]){
dp[n][d[n]]+=w[n];
auto it=dp[n].upper_bound(d[n]);
while(it!=dp[n].end()){
if(it->second<=w[n])
next(it)->second+=it->second,it=next(it),dp[n].erase(prev(it));
else {
it->second-=w[n];
break;
}
}
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n,m,k,Q;
cin>>n>>m>>k;
for(int i=2;i<=n;i++)
cin>>Q,adj[Q].push_back(i);
while(m--){
int a,b,c;
cin>>a>>b>>c;
d[a]=b,w[a]=c;
}
dfs(1);
int ans=0;
for(auto&[i,j]:dp[1])
ans+=j;
cout<<ans;
}
# | 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... |