This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;
using ll = long long;
using vl = V<ll>;
using T = map<int, ll>;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M, K; cin >> N >> M >> K;
V<vi> chd(N); for(int u = 1; u < N; u++) {
int p; cin >> p; --p;
chd[p].pb(u);
}
vi W(N, 0), D(N, -1);
for(int i = 0; i < M; i++) {
int v, d, w; cin >> v >> d >> w; --v, --d;
D[v] = d, W[v] = w;
}
function<T(int)> dfs = [&](int u) {
T R;
for(auto& v : chd[u]) {
T r = dfs(v);
for(auto p : r) R[p.first] += p.second;
}
if (D[u] != -1) {
R[D[u]] += W[u];
ll amt = 0;
while(1) {
auto it = R.upper_bound(D[u]);
if (it == end(R)) break;
amt += it->s;
if (W[u] < amt) { it->s = amt - W[u]; break; }
else R.erase(it);
}
}
return R;
};
T ans = dfs(0);
ll ANS = 0;
for(auto p : ans) ANS += p.second;
cout << ANS << nl;
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... |