#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <map>
#define int ll
using ll = long long;
#define debug(x) #x << " = " << x << '\n'
void merge(std::map<int, ll> &a, const std::map<int, ll> &b) {
  for (auto &[h, v] : a) {
    ll best = 0;
    for (const auto &[x, y] : b) {
      if (x <= h) {
        best = std::max(best, y);
      }
    }
    v += best;
  }
}
signed main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  std::cin >> n;
  std::vector<int> h(n), c(n);
  std::vector<std::vector<int>> g(n);
  ll answer = 0;
  for (int i = 0; i < n; i++) {
    int p;
    std::cin >> p >> h[i] >> c[i];
    h[i] *= -1;
    p--;
    answer += c[i];
    if (p != i) {
      g[p].push_back(i);
    }
  }
  std::vector<std::map<int, ll>> dp(n);
  auto dfs = [&](auto &&self, int u) -> void {
    dp[u][0] = 0;
    dp[u][h[u]] = c[u];
    for (int v : g[u]) {
      self(self, v);
      merge(dp[u], dp[v]);
    }
  };
  dfs(dfs, 0);
  ll maxi = 0;
  for (const auto &[x, y] : dp[0]) {
    maxi = std::max(maxi, y); 
  }
  answer -= maxi;
  std::cout << answer;
  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... |