This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// ~Be name khoda~ //
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define cl clear
#define endll '\n'
const int maxn = 1e6 + 10;
const int maxn5 = 5e3 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 2e18;
ll ans = 0;
int h[maxn5];
ll dp[maxn5][maxn5], c[maxn5];
vector <int> adj[maxn5], ex;
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n, a; cin >> n;
ll sum = 0;
for(int i = 0; i < n; i++){
cin >> a >> h[i] >> c[i];
a--;
if(i > 0){
adj[a].pb(i);
}
sum += c[i];
ex.pb(h[i]);
}
sort(all(ex));
ex.resize(unique(all(ex)) - ex.begin());
for(int i = 0; i < n; i++){
h[i] = lower_bound(all(ex), h[i]) - ex.begin();
}
int m = ex.size();
for(int i = n - 1; i >= 0; i--) {
for(int j = m - 1; j >= 0; j--){
for(auto u : adj[i])
dp[i][j] += dp[u][j];
if(h[i] != j)
dp[i][j] += c[i];
if(j < m - 1)
dp[i][j] = min(dp[i][j], dp[i][j + 1]);
}
}
cout << *min_element(dp[0], dp[0] + m) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |