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;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 200005
int n;
int h[MAXN], c[MAXN];
vi adj[MAXN], radj[MAXN], nadj[MAXN];
int rep[MAXN];
bool instk[MAXN];
map<int, ll> incre[MAXN];
ll ans;
int in[MAXN];
vi topo, stk;
bool visited[MAXN];
void dfs(int u, bool r, vi& stk) {
	vi ad = r ? radj[u] : adj[u];
	for (int v : ad) {
		if (visited[v]) continue;
		visited[v] = 1;
		dfs(v, r, stk);
	}
	stk.pb(u);
}
void dp(int u) {
	for (int v : nadj[u]) {
		dp(v);
		if (incre[v].size() > incre[u].size()) swap(incre[v], incre[u]);
		for (auto i : incre[v]) {
			incre[u][i.FI] += i.SE;
		}
	}
	auto lower = incre[u].lower_bound(h[u]);
	if (lower != incre[u].begin()) {
		lower = prev(lower);
		int remc = c[u];
		while (1) {
			//printf("%d %lld\n", lower -> FI, lower -> SE);
			ll take = min((ll) remc, lower -> SE);
			lower -> SE -= take;
			remc -= take;
			if (lower -> SE == 0) {
				auto tmp = lower;
				bool hv = 0;
				if (tmp != incre[u].begin()) {
					tmp = prev(tmp);
					hv = 1;
				}
				incre[u].erase(lower);
				if (!hv) {
				   	break;
				} else {
					lower = tmp;
				}
			} else {
				break;
			}
		}
	}
	incre[u][h[u]] += c[u];
	//printf("%d:\n", u);
	//for (auto i : incre[u]) {
		//printf(" %d %lld\n", i.FI, i.SE);
	//}
}
int main() {
	scanf("%d", &n);
	REP (i, 1, n + 1) {
		int a; scanf("%d%d%d", &a, &h[i], &c[i]);
		ans += c[i];
		if (a != i) {
		   	adj[a].pb(i);
			radj[i].pb(a);
		}
	}
	REP (i, 1, n + 1) {
		if (!visited[i]) {
			visited[i] = 1;
			dfs(i, 0, topo);
		}
	}
	memset(visited, 0, sizeof visited);
	reverse(ALL(topo));
	for (int i : topo) {
		if (!visited[i]) {
			visited[i] = 1;
			stk.clear();
			dfs(i, 1, stk);
			for (int u : stk) {
				instk[u] = 1;
			}
			sort(ALL(stk), [&] (int l, int r) {
					return h[l] > h[r];
					});
			REP (i, 1, stk.size()) {
				nadj[stk[i - 1]].pb(stk[i]);
			}
			for (int u : stk) {
				rep[u] = stk.back();
				for (int v : radj[u]) {
					if (instk[v]) continue;
					nadj[rep[v]].pb(rep[u]);
				}
			}
			for (int u : stk) {
				instk[u] = 0;
			}
		}
	}
	REP (i, 1, n + 1) {
		for (int v : nadj[i]) {
			in[v]++;
		}
	}
	//REP (i, 1, n + 1) {
		//printf("%d:", i);
		//for (int v : nadj[i]) {
			//printf(" %d", v);
		//}
		//printf("\n");
	//}
	REP (i, 1, n + 1) {
		if (in[i] != 0) continue;
		dp(i);
		for (auto i : incre[i]) {
			ans -= i.SE;
		}
	}
	printf("%lld\n", ans);
	return 0;
}
/*
4
1 3 4
1 1 2
2 0 3
3 2 1
*/
Compilation message (stderr)
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:8:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define REP(i, s, e) for (int i = s; i < e; i++)
......
  122 |    REP (i, 1, stk.size()) {
      |         ~~~~~~~~~~~~~~~~                
worst_reporter2.cpp:122:4: note: in expansion of macro 'REP'
  122 |    REP (i, 1, stk.size()) {
      |    ^~~
worst_reporter2.cpp:94:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
worst_reporter2.cpp:96:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |   int a; scanf("%d%d%d", &a, &h[i], &c[i]);
      |          ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |