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 sz(x) ((int)size(x))
#define all(x) begin(x), end(x)
#define trace(x) cout << #x << ": " << (x) << endl;
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
const int N = 100000;
map<int, ll> dp[N];
int p[N], d[N], w[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i < n; ++i) {
cin >> p[i];
--p[i];
}
for (int i = 0; i < m; ++i) {
int v;
cin >> v;
--v;
cin >> d[v] >> w[v];
}
for (int i = n - 1; i > -1; --i) {
if (d[i] != 0) {
ll sum = w[i];
dp[i][d[i]] += w[i];
while (true) {
auto it = dp[i].upper_bound(d[i]);
if (it == dp[i].end()) {
break;
}
if (it->second <= sum) {
sum -= it->second;
dp[i].erase(it);
} else {
it->second -= sum;
break;
}
}
}
if (i > 0) {
if (dp[p[i]].size() < dp[i].size())
swap(dp[i], dp[p[i]]);
for (auto [a, b] : dp[i]) {
dp[p[i]][a] += b;
}
}
}
ll ans = 0;
for (auto [a, b] : dp[0]) {
ans += b;
}
cout << ans;
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... |