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;
const int MAXN = 1e5+10;
const int INF = 1e9;
int n,m,k;
vector<int> cost;
vector<int> rday;
vector<vector<int>> g;
long long dp[MAXN][25];
long long get(int v, int md){
if(dp[v][md] != -1) return dp[v][md];
if(md == 0) return dp[v][md] = 0;
dp[v][md] = (rday[v] == md ? cost[v] : 0);
for(int z: g[v]) dp[v][md] += get(z,md);
return dp[v][md] = max(dp[v][md],get(v,md-1));
}
int main(){
cin >> n >> m >> k;
g.resize(n+1);
cost.reserve(n+1);
rday.assign(n+1,INF);
for(int i = 2,x; i <= n; ++i){
cin >> x;
g[x].emplace_back(i);
}
int cnt = 0;
long long sum = 0;
for(int i = 0,v; i < m; ++i) {
cin >> v; cin >> rday[v] >> cost[v];
if(g[v].size() == 0) {
cnt++;
sum += cost[v];
}
}
if(cnt == m){
cout << sum;
return 0;
}
for(int i = 0; i <= n; ++i){
for(int j = 0; j <= k+1; ++j){
dp[i][j] = -1;
}
}
cout << get(1,k+1);
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... |