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 TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long ll;
//const int mxN = 2e5+5;
const int mxN = 5e3+5;
const ll inf = 1e18;
int N, A[mxN], H[mxN], C[mxN];
vector<int> al[mxN];
ll dp[mxN], sum[mxN];
ll best(int u, int x) {
ll ret = 0;
for (int v : al[u]) {
ret += best(v,x);
}
if (H[x] <= H[u]) {
ret = min(ret, dp[u]-sum[u]);
}
return ret;
}
void dfs(int u) {
sum[u] = 0;
dp[u] = 0;
for (int v : al[u]) {
dfs(v);
sum[u] += sum[v];
dp[u] += best(v,u);
}
dp[u] += sum[u];
sum[u] += C[u];
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N;
FOR(i,1,N){
cin >> A[i] >> H[i] >> C[i];
if (i != 1) al[A[i]].push_back(i);
}
H[0] = 1;
dfs(1);
cout << sum[1] + best(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... |