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;
using ll = long long;
const int N = 100100;
int n, m, k;
int p[N];
int d[N];
int w[N];
map<int, ll> mp[N];
vector<int> g[N];
int unite(int i, int j) {
if ((int)mp[i].size() < (int)mp[j].size()) swap(i, j);
for (auto c : mp[j])
mp[i][c.first] += c.second;
return i;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 2; i <= n; i++) {
cin >> p[i];
g[p[i]].push_back(i);
}
for (int i = 0; i < m; i++) {
int v;
cin >> v;
cin >> d[v] >> w[v];
}
for (int v = n; v >= 1; v--) {
int heavy = 0;
for (int c : g[v])
if ((int)mp[c].size() > (int)mp[heavy].size())
heavy = c;
swap(mp[heavy], mp[v]);
for (int c : g[v])
unite(v, c);
auto it = mp[v].lower_bound(d[v]);
int s = w[v];
while (it != mp[v].end()) {
if (it->second <= s) {
s -= it->second;
mp[v].erase(it);
} else {
mp[v][it->first] = it->second - s;
break;
}
it = mp[v].lower_bound(d[v]);
}
mp[v][d[v]] += w[v];
// cerr << "vertex: " << v << "\n";
// cerr << "{";
// for (auto c : mp[v]) cout << '(' << c.first << ' ' << c.second << ')' << ' ';
// cerr << "}\n";
}
ll s = 0;
for (auto c : mp[1])
s += c.second;
cout << s << '\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... |