#include #include #include #include #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector>> g(n); for (int i = 0; i + 1 < n; ++i) { long long a, b; int x; cin >> a >> b >> x; --x; g[i].push_back({i + 1, a}); g[i].push_back({x, b}); } const long long inf = numeric_limits::max() / 4; vector dist(n, inf); dist[0] = 0; priority_queue, vector>, greater<>> pq; pq.push({0, 0}); while (!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if (d != dist[u]) continue; for (auto [v, w] : g[u]) if (dist[v] > d + w) { dist[v] = d + w; pq.push({dist[v], v}); } } cout << dist[n - 1] << '\n'; }