#pragma GCC optimize("Ofast,unroll-all-loops")
#include <bits/stdc++.h>
using namespace std;
const int64_t inf = 1e15;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<vector<int>> adj(n + 1);
vector<int> h(n + 1), c(n + 1), buff;
for (int i = 1, a; i <= n; ++i) {
cin >> a >> h[i] >> c[i];
if (a != i) {
adj[a].push_back(i);
}
buff.push_back(h[i]);
}
sort(buff.begin(), buff.end());
buff.erase(unique(buff.begin(), buff.end()), buff.end());
auto compr = [&](int i) { return lower_bound(buff.begin(), buff.end(), i) - buff.begin(); };
for (int i = 1; i <= n; ++i) {
h[i] = compr(h[i]);
}
auto dfs = [&](auto &&self, int u) -> map<int, int64_t> {
map<int, int64_t> dp;
dp[0] = c[u], dp[h[u]] = 0, dp[h[u] + 1] = c[u];
auto split = [&](int l) {
auto it = --dp.upper_bound(l);
if (it->first != l) {
dp[l] = it->second;
}
};
auto add = [&](int l, int r, int64_t val) {
split(l), split(r);
for (auto it = dp.lower_bound(l); it != dp.end() && it->first < r; ++it) {
it->second += val;
}
};
for (int &i : adj[u]) {
auto ch = self(self, i);
if (dp.size() < ch.size()) {
swap(dp, ch);
}
for (auto it = ch.begin(); it != ch.end(); ++it) {
add(it->first, it == --ch.end() ? n : next(it)->first, it->second);
}
}
while (!dp.empty() && (--dp.end())->first >= n) {
dp.erase(--dp.end());
}
for (auto it = --dp.end(); it != dp.begin(); --it) {
prev(it)->second = min(prev(it)->second, it->second);
}
map<int, int64_t> clean;
for (auto it = dp.begin(); it != dp.end(); ++it) {
if (it == dp.begin() || prev(it)->second != it->second) {
clean[it->first] = it->second;
}
}
return clean;
};
cout << dfs(dfs, 1).begin()->second << '\n';
}