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 pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n;
vector<vector<int>> v;
vector<vector<ll>> dp;
vector<int> h;
vector<ll> cost;
void dfs(int nd){
for(int x: v[nd]) dfs(x);
for(int x: v[nd]) for(int i = 0; i < n; i++) dp[nd][i]+=dp[x][i];
for(int i = h[nd]+1; i < n; i++){
dp[nd][i]+=cost[nd];
}
for(int i = h[nd]-1; i >= 0; i--){
dp[nd][i] = min(dp[nd][i+1], dp[nd][i]+cost[nd]);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
v.resize(n);
dp.resize(n, vector<ll>(n, 0));
h.resize(n);
cost.resize(n);
for(int i = 0; i < n; i++){
int p; cin >> p >> h[i] >> cost[i];
p--;
if(p == i) continue;
v[p].pb(i);
}
/*compress h*/{
map<int, int> mp;
vector<int> srt = h;
sort(srt.begin(), srt.end());
int in = 0;
for(int i = 0; i < n; i++) mp[srt[i]] = in, in++;
for(int &x: h) x = mp[x];
}
dfs(0);
cout << *min_element(dp[0].begin(), dp[0].end()) << "\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... |