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;
#define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> adj(n+1);
vector<int> H(n+1);
vector<int> Hs(n+1);
vector<int> C(n+1);
int a;cin>>a>>H[1]>>C[1];Hs[1]=H[1];
for(int i=2;i<=n;i++){
cin>>a>>H[i]>>C[i];Hs[i]=H[i];
adj[a].emplace_back(i);
}
sort(Hs.begin(),Hs.end());
Hs.erase(unique(Hs.begin(),Hs.end()),Hs.end());
int k = Hs.size();
function<vector<int>(int)> dfs = [&](int x){
vector<int> DP(k);
for(int i=0;i<k;i++)if(Hs[i]!=H[x])DP[i]=C[x];
for(int&i:adj[x]){
auto curr = dfs(i);
for(int j=0;j<k;j++)DP[j]+=curr[j];
}
for(int i=k-2;i>=0;i--)DP[i]=min(DP[i],DP[i+1]);
return DP;
};
cout << dfs(1)[0] << '\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... |