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 ll long long
#define pb push_back
const int mxN = 1e5 + 5;
ll n,m,k;
pair<int,int> a[mxN];
vector<int> v[mxN];
map<int, ll> dp[mxN];
void dfs(ll at, ll p){
for(auto it : v[at]){
if(it == p) continue;
dfs(it, at);
if(dp[it].size() > dp[at].size()) swap(dp[it], dp[at]);
for(auto it1 : dp[it]) dp[at][it1.first] += it1.second;
dp[it].clear();
}
dp[at][a[at].first] += a[at].second;
ll left = a[at].second;
for(auto it = dp[at].upper_bound(a[at].first); it != dp[at].end();){
if((*it).second > left){
(*it).second -= left;
break;
}
left -= (*it).second;
dp[at].erase(it++);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for(int i = 2; i <= n; i++){
ll p;
cin >> p;
v[p].pb(i);
v[i].pb(p);
}
for(int i = 0; i < m; i++){
ll idx,d,w;
cin >> idx >> d >> w;
a[idx] = {d, w};
}
dfs(1, 1);
ll ans = 0;
for(auto it : dp[1]) ans += it.second;
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... |