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;
using ll = long long;
#define all(x) (x).begin(), (x).end()
const int N = 5007;
int n;
int h[N], c[N];
vector<int> adj[N];
ll dp[N][N];
void dfs(int v = 1) {
for (int u : adj[v]) {
dfs(u);
for (int i = 1; i <= n; i++)
dp[v][i] += dp[u][i];
}
for (int i = 1; i <= h[v]; i++)
dp[v][i] = max(dp[v][i], dp[v][h[v]] + c[v]);
}
int main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n;
ll sum = 0;
vector<int> cmp = {0};
for (int i = 1; i <= n; i++) {
int p;
cin >> p >> h[i] >> c[i];
if (i != 1)
adj[p].push_back(i);
cmp.push_back(h[i]);
sum += c[i];
}
sort(all(cmp));
cmp.erase(unique(all(cmp)), cmp.end());
for (int i = 1; i <= n; i++)
h[i] = lower_bound(all(cmp), h[i]) - cmp.begin();
dfs();
cout << sum - *max_element(dp[1] + 1, dp[1] + n + 1) << '\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... |