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++14 (judge verdict: Wrong Answer)

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
int main(){
    int n;
    while(cin >> n &&n!=0){
        vector<vector<pair<ll,int> > > g(n);
        vector<int> p(n);
        vector<ll>d(n);
        for(int i=1;i<n;i++){
            cin >> p[i];
            p[i]--;
        }
        ll ans=0;
        for(int i=1;i<n;i++){
            cin >> d[i];
            ans+=d[i];
        }
        ans *=3;
        for(int i=1;i<n;i++){
            g[i].PB(MP(d[i],p[i]));
            g[p[i]].PB(MP(d[i],i));
        }
        ll s = 0;
        vector<bool>flag(n);
        for(int i=0;i<n;i++){
            if(g[i].size()==1){
                s += g[i][0].first;
                flag[i] = 1;
            }
        }
        priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> >  >pq;
        for(int i=0;i<n;i++){
            if(g[i].size()!=1){
                pq.push(MP((ll)0,i));
                flag[i] = 1;
                break;
            }
        }
        int l=-1;
        while(!pq.empty()){
            auto a = pq.top();
            l = a.second;
            pq.pop();
            for(auto x:g[l]){
                if(!flag[x.second]){
                    flag[x.second] = 1;
                    pq.push(MP(a.first+x.first,x.second));
                }
            }
        }
        rep(i,n)if(g[i].size()!=1)flag[i]=0;
        if(l!=-1)pq.push(MP((ll)0,l)),flag[l]=1;
        ll r = 0;
        while(!pq.empty()){
            auto a = pq.top();
            r = a.first;
            pq.pop();
            for(auto x:g[a.second]){
                if(!flag[x.second]){
                    flag[x.second] = 1;
                    pq.push(MP(a.first+x.first,x.second));
                }
            }
        }
        cout << ans << " " << s << " " <<r << endl;
        ans -= 2*s+r;
        cout <<ans << endl;
    }
    return 0;
}