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;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e5+10;
int n, m, k;
pair<int,int> frut[maxn];
vector<int> g[maxn];
map<int,int> dp[maxn];
void solve() {
cin >> n >> m >> k;
for(int i = 2; i <= n; i++) {
int v; cin >> v;
g[v].pb(i);
}
for(int i = 1; i <= m; i++) {
int v,d,w; cin >> v >> d >> w;
frut[v] = mp(d,w);
}
for(int u = n; u >= 1; u--) {
dp[u][0] = 0;
for(auto v : g[u]) {
if(dp[v].size() > dp[u].size()) swap(dp[u],dp[v]);
for(auto x : dp[v]) {
dp[u][x.fr]+= x.sc;
}
}
if(frut[u].fr == 0) continue;
int d = frut[u].fr;
int w = frut[u].sc;
int sum = 0;
auto it = dp[u].upper_bound(d);
vector<int> era;
while(it != dp[u].end()) {
int d1 = it->fr;
int w1 = it->sc;
sum+= w1;
if(sum > w) {
dp[u][d1] = -w+sum;
break;
}
else {
era.pb(d1);
it = next(it);
}
}
dp[u][d]+= w;
for(auto x : era) dp[u].erase(x);
// cout << u << endl;
// for(auto x : dp[u]) {
// cout << " " << x.fr << " " << x.sc << endl;
// }
}
int ans = 0;
for(auto x : dp[1]) ans+= x.sc;
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# | 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... |