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;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 5e3 + 10;
int n, H[MAXN], C[MAXN];
ll dp[MAXN][MAXN], sum;
vector<int> adj[MAXN], vec;
void dfs(int v) {
for (int u : adj[v]) {
dfs(u);
for (int i = 1; i <= n; i++) {
dp[v][i] += dp[u][i];
}
}
dp[v][H[v]] += C[v];
for (int i = n - 1; i > 0; i--)
dp[v][i] = max(dp[v][i], dp[v][i + 1]);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int p;
cin >> p;
if (i > 1) adj[p].push_back(i);
cin >> H[i] >> C[i];
vec.push_back(H[i]);
sum += C[i];
}
sort(all(vec));
for (int i = 1; i <= n; i++)
H[i] = lower_bound(all(vec), H[i]) - vec.begin() + 1;
dfs(1);
cout << sum - dp[1][1] << endl;
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... |