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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,mm,k;
vector<int> al[100005];
ii fruit[100005];
map<int,ll> m[100005];
void dfs(int i){
for (auto &it:al[i]){
dfs(it);
//map merging :thinking:
if (sz(m[i])<sz(m[it])) swap(m[i],m[it]);
for (auto &item:m[it]){
if (m[i].count(item.fi)) m[i][item.fi]+=item.se;
else m[i][item.fi]=item.se;
}
}
if (fruit[i]==ii(0,0)) return;
map<int,ll>::iterator it;
if (m[i].count(fruit[i].fi)){
it=m[i].find(fruit[i].fi);
}
else{
it=m[i].insert({fruit[i].fi,0}).fi;
}
it->se+=fruit[i].se;
ll curr=fruit[i].se;
while (next(it)!=m[i].end()){
int temp=min(next(it)->se,curr);
if (temp<curr){
m[i].erase(next(it));
curr-=temp;
}
else{
next(it)->se-=temp;
break;
}
}
//cout<<"debug: "<<i<<endl;
//for (auto &it:m[i]) cout<<it.fi<<"_"<<it.se<<" ";cout<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>mm>>k;
rep(x,2,n+1){
int p;
cin>>p;
al[p].push_back(x);
}
rep(x,0,mm){
int a,b,c;
cin>>a>>b>>c;
fruit[a]={b,c};
}
dfs(1);
ll ans=0;
for (auto &it:m[1]) ans+=it.se;
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... |