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 <utility>
#include <algorithm>

using namespace std;
vector<vector<pair<int,int>>> es;
vector<int> depth;

template <typename T>
std::ostream& operator<<(ostream&os, const vector<T>& v) 
{ 
   os<<"(";
   for (typename vector<T>::const_iterator it = v.begin() ; it != v.end() ; ++it )
       os << *it << ", ";
   return os<<")"; 
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S>&v) {
  os << "(" << v.first << ", " << v.second << ")";
}

void print() {
  for(const vector<pair<int,int>> & esx : es) {
//    cout << esx << endl;
    for(const pair<int,int>& a : esx) {
      cout << a << ", ";
      //cout << "(" << a.first << ", " << a.second << ")" << ", ";
    }
    cout << endl;
  }
}  

// recursion: contracting leaves
bool rec(int x, int prnt) {
  if(es[x].size() == 1) { // this is a leaf
    es[x].clear();
    return true;
  } else {
    vector<pair<int,int>> esx;
    for(auto p : es[x]) {
      if(p.first == prnt || !rec(p.first, x)) {
        esx.push_back(p);
      }
    }
    es[x] = esx;
    return false;
  }
}

// dfs
bool dfs(int x, int prnt) {
  for(auto p : es[x]) {
    if(p.first == prnt) continue;
    depth[p.first] = depth[x] + p.second;
    dfs(p.first, x);
  }
}

int main(int argc, char *argv[])
{
  for(;;) {
    int n;
    cin >> n;
    if(n == 0) break;
    vector<int> ps, ds;
    for(int i = 0; i < n - 1; i++) {
      int p;
      cin >> p;
      p--;
      ps.push_back(p);
    }
    int dsum = 0;  // sum of all edge lengths = the essential cost
    for(int i = 0; i < n - 1; i++) {
      int d;
      cin >> d;
      dsum += d;
      ds.push_back(d);
    }
    es.clear();
    for(int i = 0; i < n; i++) {
      es.push_back(vector<pair<int,int>>());
      
    }
    for(int i = 0; i < n - 1; i++) {
      int p = ps[i];
      int d = ds[i];
      es[i+1].push_back(pair<int,int>(p,d));
      es[p].push_back(pair<int,int>(i+1,d));
    }
    //print();
    // make a root
    int r = 0;
    while(es[r].size() < 2) r++;
    
    // contract the leaves; these can be removed without moving
    rec(r, -1);
    //print();

    // then, the cost is "dsum + the total remaining edges - the logest path"
    //   trivially, we can remove all bridges by moving around the remaining tree; this results in the cost "dsum + the total remaining edges".
    //   how can we reduce the cost? the starting and ending islands can be diffetent, and this means that we may reduce the cost by that of the 'return' trip.

    // total of the remaining edges (both direction) (= the length of the tour)
    int esum = 0;
    for(auto esx : es) {
      for(auto v : esx) {
        esum += v.second;
      }
    }

    // distance from the root
    depth = vector<int>(n, 0);
    dfs(r, -1);

    // the longest = the sum of the top two
    sort(depth.begin(), depth.end());
    // cout << depth << endl;
    int ls = depth[n-1] + depth[n-2];
    cout << dsum + esum - ls << endl;
  }
}