This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 5000;
int N;
int A[N_MAX + 2], H[N_MAX + 2], C[N_MAX + 2];
vector <int> sons[N_MAX + 2];
int p[N_MAX + 2];
ll dp[N_MAX + 2][N_MAX + 2];
ll dps[N_MAX + 2][N_MAX + 2];
void dfs (int u)
{
for(int v : sons[u])
dfs(v);
for(int i = 1; i <= N; i++)
{
for(int v : sons[u])
dp[u][i] += dps[v][i];
if(H[u] != H[p[i]])
dp[u][i] += C[u];
dps[u][i] = dp[u][i];
if(i > 1)
dps[u][i] = min(dps[u][i], dps[u][i - 1]);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for(int u = 1; u <= N; u++)
cin >> A[u] >> H[u] >> C[u];
for(int u = 2; u <= N; u++)
sons[A[u]].push_back(u);
for(int i = 1; i <= N; i++)
p[i] = i;
sort(p + 1, p + N + 1, [&] (const int &i, const int &j)
{
return H[i] > H[j];
});
dfs(1);
cout << dps[1][N] << "\n";
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... |