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;
vector< vector<int> > g;
vector<int> d;
vector<long long> w;
vector< map<long long, long long> > dp;
void dfs(int u) {
for (auto& v : g[u]) {
dfs(v);
if ((int) dp[u].size() < (int) dp[v].size()) {
swap(dp[u], dp[v]);
}
for (auto& p : dp[v]) {
dp[u][p.first] += p.second;
}
}
dp[u][d[u]] += w[u];
auto it = dp[u].upper_bound(d[u]);
long long tmp = w[u];
while (it != dp[u].end()) {
if (it->second > tmp) {
it->second -= tmp;
break;
}
tmp -= it->second;
dp[u].erase(it++);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
g.resize(n + 1);
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
g[p].push_back(i);
}
d.assign(n + 1, 1);
w.resize(n + 1);
for (int i = 0; i < m; i++) {
int v;
cin >> v;
cin >> d[v] >> w[v];
}
dp.resize(n + 1);
dfs(1);
long long ans = 0;
for (auto& p : dp[1]) {
ans += p.second;
}
cout << ans << '\n';
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... |