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 x first
#define y second
#define pb push_back
#define int long long
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
const int N = 2e5 + 7;
int n;
int par[N], h[N], c[N];
vector<int> g[N];
long long dp[N], sum[N];
long long get(int node, int lim) {
long long sol = c[node];
for (int v : g[node]) sol += get(v, lim);
if (h[node] >= lim) sol = min(sol, dp[node]);
return sol;
}
void dfs(int u) {
sum[u] = c[u];
dp[u] = 0;
for (int v : g[u]) {
dfs(v);
sum[u] += sum[v];
dp[u] += get(v, h[u]);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> par[i] >> h[i] >> c[i];
for (int i = 2; i <= n; ++i) g[par[i]].push_back(i);
dfs(1);
cout << get(1, 0);
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... |