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;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 1e5 + 5;
vector<int> g[N];
pair<int, long long> a[N];
map<int, long long> dp[N];
void merge(map<int, long long>& du, map<int, long long>& dv) {
if (du.size() > dv.size()) {
swap(du, dv);
}
for (auto it : du) {
dv[it.first] += it.second;
}
}
void dfs(int v) {
for (int u : g[v]) {
dfs(u);
merge(dp[u], dp[v]);
}
if (!a[v].first) return;
long long tmp = a[v].second;
dp[v][a[v].first] += a[v].second;
auto it = dp[v].upper_bound(a[v].first);
while (tmp && it != dp[v].end()) {
if (tmp >= it->second) {
tmp -= it->second;
dp[v].erase(it);
} else {
it->second -= tmp;
tmp = 0;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
for (int i = 2; i <= n; ++i) {
int p;
cin >> p;
g[p].push_back(i);
}
for (int i = 0; i < m; ++i) {
int v, t;
long long w;
cin >> v >> t >> w;
a[v] = {t, w};
}
dfs(1);
long long ans = 0;
for (auto it : dp[1]) {
ans += it.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... |