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<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
#include<tuple>
#include<bitset>
using namespace std;
#define MOD 1000000007
#define int long long
#define ff first
#define ss second
#define endl '\n'
const int mxN=1e5+1;
map<int,int> mp[mxN];
vector<int> v[mxN],d(mxN),w(mxN);
void dfs(int a){
for(auto node: v[a]){
dfs(node);
if(mp[a].size()<mp[node].size()) swap(mp[a],mp[node]);
for(auto x: mp[node]) mp[a][x.ff]+=x.ss;
}
if(d[a]>0){
mp[a][d[a]]+=w[a];
while(true){
auto it=mp[a].upper_bound(d[a]);
if(it==mp[a].end()) break;
if(it->ss<w[a]){
w[a]-=it->ss;
mp[a].erase(it);
continue;
}
it->ss-=w[a];
break;
}
}
}
void solve(){
int n,m,k; cin>>n>>m>>k;
for(int i=2; i<=n; i++){
int a; cin>>a;
v[a].push_back(i);
}
for(int i=1; i<=m; i++){
int a; cin>>a;
cin>>d[a]>>w[a];
}
dfs(1);
int ans=0;
for(auto x: mp[1]) ans+=x.ss;
cout<<ans;
}
signed main(){
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
cout<<endl;
}
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... |