/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.0.2"
+/

import std.algorithm, std.range, std.stdio, std.math, std.conv, core.bitop;

import dcomp.scanner;
import dcomp.algorithm;

int n;
struct Edge {
    int to;
    long dist;
}
Edge[][] g;
long md;

T[N] toS(T, int N)(T[N] data){ return data; }

long[2][][] memo;
long[2][] dfs(int p) {
    if (g[p].empty) {
        memo[p].length = 1;
        memo[p][0] = [0L, 0L];
        return memo[p];
    }
    Edge le = g[p][0], re = g[p][1];
    auto lt = dfs(le.to);
    auto rt = dfs(re.to);
    if (lt.length > rt.length) {
        swap(le, re);
        swap(lt, rt);
    }
    long ld = le.dist, rd = re.dist;
    auto buf = Appender!(long[2][])(memo[p]);
    buf.clear();
    foreach (co; lt) {
        //co[1] + ld + rd + rco[0] <= md
        int idx = binSearch!((i){return co[1]+ld+rd+rt[i][0] > md;})(-1L, rt.length).to!int-1;
        if (idx == -1) continue;
        buf.put([co[0]+ld, rt[idx][1]+rd].toS);
        buf.put([rt[idx][1]+rd, co[0]+ld].toS);
    }
    memo[p] = buf.data;
    sort(memo[p]);
    if (memo[p].empty) return memo[p];
    long ma = 10L^^15;
    int co = 0;
    foreach (e; memo[p]) {
        if (ma <= e[1]) continue;
        ma = min(ma, e[1]);
        memo[p][co] = e;
        co++;
    }
    return memo[p][0..co];
}

//md以下にできるか？
bool solve(long _md) {
    md = _md;
    return !dfs(0).empty;
}

int main() {
    auto sc = new Scanner();
    int n;
    sc.read(n);
    g = new Edge[][](n);
    memo = new long[2][][](n);
    foreach (i; 1..n) {
        int a; long b;
        sc.read(a, b); a--;
        g[a] ~= Edge(i, b);
    }

    writeln(binSearch!solve(-1L, 10L^^12));
    return 0;
}
