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>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 1e5 + 7;
const int Mod = 1e9 + 7;
const int szBL = 320;
const int INF = 1e9;
const int BASE = 137;
int n, m, K;
int D[N];
ll W[N];
vector<int> ke[N];
map<int, ll> dp[N];
int szC[N];
void dfs (int u, int p) {
szC[u] = 1;
int mxV = -1;
iter (&v, ke[u]) {
if (v == p) continue;
dfs(v, u);
if (mxV == -1 || szC[v] > szC[mxV]) mxV = v;
szC[u] += szC[v];
}
if (mxV != -1) {
swap(dp[u], dp[mxV]);
}
iter (&v, ke[u]) {
if (v == p || v == mxV) continue;
iter (&id, dp[v]) {
dp[u][id.fs] += id.se;
}
map<int,ll> ().swap(dp[v]);
}
ll val = W[u];
dp[u][D[u]] += val;
auto it = dp[u].lower_bound(D[u] + 1);
while (it != dp[u].end()) {
if (it->se <= val) {
val -= it->se;
it = dp[u].erase(it);
}
else {
it->se -= val;
break;
}
}
}
/*
(i, dp[i] - dp[i - 1]);
*/
void solution () {
cin >> n >> m >> K;
rep (i, 1, n - 1) {
int u, v;
cin >> v;
u = i + 1;
ke[u].push_back(v);
ke[v].push_back(u);
}
rep (i, 1, m) {
ll v, d, w;
cin >> v >> d >> w;
D[v] = d;
W[v] = w;
}
dfs(1, 0);
dp[1][K] += 0;
for(auto it = dp[1].begin(); it != prev(dp[1].end()); ++it) {
next(it)->se += it->se;
}
cout << dp[1][K] <<"\n";
}
#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
no bug challenge
*/
# | 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... |