This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//BITARO BITARO BITARO
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<int> adj[N];
map<int64_t, int64_t> mp[N];
int64_t d[N], w[N], ans;
void dfs(int u) {
for(int v: adj[u]) {
dfs(v);
if(mp[v].size() > mp[u].size()) swap(mp[u], mp[v]);
for(auto x: mp[v]) mp[u][x.first] += x.second;
}
if(!d[u]) return;
mp[u][d[u]] += w[u];
int64_t val = w[u];
while(val > 0) {
auto it = mp[u].upper_bound(d[u]);
if(it == mp[u].end()) break;
int64_t vv = it->second;
if(val >= vv) {
mp[u].erase(it);
} else {
it->second -= val;
}
val -= vv;
}
}
int main() {
int n, m, k; cin >> n >> m >> k;
for(int i = 2; i <= n; ++i) {
int x; cin >> x;
adj[x].push_back(i);
}
for(int i = 0; i < m; ++i) {
int a, b, c; cin >> a >> b >> c;
d[a] = b;
w[a] = c;
}
dfs(1);
for(auto x: mp[1]) ans += x.second;
cout << ans << "\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... |