Problem:
Bridge Removal

ICPC islands once had been a popular tourist destination.
For nature preservation, however, the government decided to prohibit entrance to the islands,
and to remove all the man-made structures there.
The hardest part of the project is to remove all the bridges connecting the islands.

There are n islands and n-1 bridges.
The bridges are built so that all the islands are reachable from all the other islands by
crossing one or more bridges.
The bridge removal team can choose any island as the starting point, and can repeat either of the
following steps.

Move to another island by crossing a bridge that is connected to the current island.

Remove one bridge that is connected to the current island, and stay at the same island after the removal.

Of course, a bridge, once removed, cannot be crossed in either direction.
Crossing or removing a bridge both takes time proportional to the length of the bridge.
Your task is to compute the shortest time necessary for removing all the bridges.
Note that the island where the team starts can differ from where the team finishes the work.

Input

The input consists of at most 100 datasets.
Each dataset is formatted as follows.

n

p2 p3 ... pn

d2 d3 ... dn

The first integer n (3 ≤ n ≤ 800) is the number of the islands.
The islands are numbered from 1 to n.
The second line contains n-1 island numbers pi
(1 ≤ pi < i), and tells that for each i
from 2 to n the island i and the island pi are connected by a bridge.
The third line contains n-1 integers di (1 ≤ di ≤ 100,000)
each denoting the length of the corresponding bridge.
That is, the length of the bridge connecting the island i and pi
is di. It takes di units of time to cross the bridge, and
also the same units of time to remove it.

Note that, with this input format, it is assured that all the islands are reachable each other by
crossing one or more bridges.

The input ends with a line with a single zero.

Output

For each dataset, print the minimum time units required to remove all the bridges in a single line.
Each line should not have any character other than this number.

Sample Input

4
1 2 3
10 20 30
10
1 2 2 1 5 5 1 8 8
10 1 1 20 1 1 30 1 1
3
1 1
1 1
0

Output for the Sample Input

80
136
2

Solution in C++11 (judge verdict: Runtime Error)

#define _CRT_SECURE_NO_WARNINGS
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template<class T> ostream & operator<<(ostream & os, vector<T> const &);
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple<T...> const &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple<T...> const & t){ os << (n==0?"":" ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream & operator<<(ostream & os, tuple<T...> const & t){ _ot<0>(os, t); return os; }
template<class T, class U> ostream & operator<<(ostream & os, pair<T,U> const & p){ return os << "(" << p.first << ", " << p.second << ") "; }
template<class T> ostream & operator<<(ostream & os, vector<T> const & v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; }
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<mt(__VA_ARGS__)<<" ["<<__LINE__<<"]"<<endl)
#else
#define dump(...)
#endif

typedef int Weight;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src_, int dst_, Weight weight_) :
        src(src_), dst(dst_), weight(weight_) { }
};
bool operator < (const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
        e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

int const inf = 1<<29;

typedef pair<Weight, int> Result;
Result visit(int p, int v, const Matrix & g) {
    Result r(0,v);
    int n = g.size();
    rep(i,n){
        if(i!=p){
            if(g[v][i]==inf) continue;
            Result t = visit(v, i, g);
            t.first += g[v][i];
            if (r.first < t.first) r = t;
        }
    }
    return r;
}
tuple<int,int,Weight> diameter(const Matrix & g, int start) {
    Result r = visit(-1, start, g);
    Result t = visit(-1, r.second, g);
    return mt(r.second, t.second, t.first); // (r.second, t.second) is farthest pair
}

Matrix mat;
int deg[1000];
int n;

int solve(int p, int v){
    int res = 0;
    rep(i,n){
        if(i==p) continue;
        if(mat[v][i]==inf) continue;
        res += mat[v][i];
        res += solve(v,i);
    }
    return res;
}

int main(){
#ifdef LOCAL
    freopen("in","r",stdin);
#endif

    while(cin >> n && n){
        mat.assign(n,Array(n,inf));
        int ans = 0;
        rep(i,n) deg[i] = 0;
        {
            int p[800], d[100];
            rep(i,n-1) cin >> p[i];
            rep(i,n-1) cin >> d[i];
            rep(i,n-1){
                p[i]--;
                mat[i+1][p[i]] = mat[p[i]][i+1] = d[i];
                deg[i+1]++;
                deg[p[i]]++;
            }
        }

        int t = 0;
        rep(i,n){
            if(deg[i]==1){
                rep(j,n){
                    if(mat[i][j]!=inf){
                        t += mat[i][j];
                        mat[i][j] = inf;
                        mat[j][i] = inf;
                    }
                }
            }
        }

        ans += t;
        int u = 0;
        rep(i,n){
            deg[i] = 0;
            rep(j,n){
                if(mat[i][j]!=inf){
                    deg[i]++;
                    u += mat[i][j];
                }
            }
        }
        ans += u/2*3;

        int start = -1;
        rep(i,n){
            if(deg[i]==0) continue;
            start = i;
            break;
        }
        if(start==-1){
            cout << ans << endl;
        } else {
            int a,b,d;
            vi dist,prev,path;
            tie(a,b,d) = diameter(mat,start);
            ans -= d;
            cout << ans << endl;
        }
    }
}