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;
using ll = long long;
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> child(n);
vector par(n, -1);
for (int i = 1; i < n; i++) {
cin >> par[i];
child[--par[i]].push_back(i);
}
map<int,int> comp;
vector<pair<int,int>> fruit(n);
for (int i =0; i < m;i ++) {
ll v, d, w;
cin >> v >> d >> w;
v--;
comp[d] = 0;
fruit[v] = {d, w};
}
k = 0;
for (auto& mp: comp) mp.second = ++k;
for (auto& [d, w] : fruit)
d = comp[d];
vector memo(n, vector(k+1, -1ll));
auto dp = [&](auto& dp, int i, int day)->ll{
if (day <= 0) return 0;
if (memo[i][day] !=-1) return memo[i][day];
ll res = 0ll;
if (fruit[i].first == day) res += fruit[i].second;
for (int e : child[i]) {
res += dp(dp, e, day);
}
if (fruit[i].first) {
if (day > fruit[i].first)
res = max(res, dp(dp, i, fruit[i].first));
else if (day == fruit[i].first)
res = max(res, dp(dp, i, fruit[i].first-1));
}
return memo[i][day] = res;
};
cout << dp(dp, 0, k) << '\n';
int bad = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= k; j++)
bad += memo[i][j] == -1;
}
cerr << n*k - bad << ' ' << bad / double(n * k) << '\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... |