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 = 1e18;
const int nax = 5003;
long long val[nax], cost[nax];
vector<int>g[nax];
int n;
bool root[nax];
long long dp[nax][nax];
long long solve(int v, int prv) {
long long &ret = dp[v][prv];
if(ret!=-1) return ret;
ret = INF;
if(!prv || val[v]>=val[prv]) {
ret = 0;
for(int u : g[v]) {
ret += solve(u, v);
}
}
long long an = cost[v];
for(int u : g[v]) {
an += solve(u, prv);
}
ret = min(ret, an);
return ret;
}
void PlayGround() {
cin>>n;
for(int i=1; i<=n; ++i) root[i] = 1;
for(int i=1; i<=n; ++i) {
int to;
cin>>to>>val[i]>>cost[i];
if(to-i) g[to].push_back(i), root[i] = 0;
}
long long ans = 0;
memset(dp, -1, sizeof dp);
for(int i=1; i<=n; ++i) if(root[i]) {
ans += solve(i, 0);
}
cout<<ans<<'\n';
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
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... |