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 ar array
typedef int64_t ll;
#define int ll
const int N = 1e5 + 5;
vector<int> edges[N];
int is[N], d[N], w[N], mx[N];
map<int, int> sub[N];
void dfs(int u){
mx[u] = u;
for(auto x : edges[u]){
dfs(x);
if(sub[x].size() > sub[mx[u]].size()) mx[u] = x;
}
swap(sub[u], sub[mx[u]]);
for(auto x : edges[u]){
if(x == mx[u]) continue;
for(auto v : sub[x]){
sub[u][v.first] += v.second;
}
sub[x].clear();
}
if(is[u]){
sub[u][d[u]] += w[u];
auto it = sub[u].upper_bound(d[u]);
vector<int> er;
while(it != sub[u].end()){
w[u] -= (*it).second;
if(w[u] <= 0){
if(w[u]) sub[u][(*it).first] = -w[u];
break;
}
er.push_back((*it).first);
}
for(auto x : er){
sub[u].erase(x);
}
}
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m, k; cin>>n>>m>>k;
for(int i=2;i<=n;i++){
int x; cin>>x;
edges[x].push_back(i);
}
for(int i=0;i<m;i++){
int v; cin>>v;
cin>>d[v]>>w[v];
is[v] = 1;
}
dfs(1);
int sum = 0;
for(auto x : sub[1]) sum += x.second;
cout<<sum<<"\n";
}
# | 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... |