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 (judge verdict: Accepted)

// Aizu 1196: Bridge Removal
// 2017.9.24 bal4u@uu

#include <stdio.h>
#include <string.h>

typedef struct { char f; int len, to[802], d[802]; } T;
T tbl[802];
int n;
int p[802], d[802];

int calc(int k)
{
	int i, j, max, s;

	if (tbl[k].f) return 0;
	tbl[k].f = 1;
	for (max = 0, i = 0; i < tbl[k].len; i++) {
		if ((j = tbl[k].to[i]) >= 0 && tbl[j].f == 0) {
			s = tbl[k].d[i] + calc(j);
			if (s > max) max = s;
		}
	}
	return max;
}

int main()
{
	int i, j, a, b, t, bridge, sum, max;
	T *q;

	while (scanf("%d", &n) && n) {
		memset(tbl, 0, sizeof(tbl));
		for (i = 1; i < n; i++) scanf("%d", &t), p[i] = t-1;
		for (sum = 0, i = 1; i < n; i++) scanf("%d", &t), d[i] = t, sum += t;
		for (i = 1; i < n; i++) {
			a = i, b = p[i];
			q = tbl + a, q->to[q->len] = b, q->d[q->len++] = d[i];
			q = tbl + b, q->to[q->len] = a, q->d[q->len++] = d[i];
		}
		bridge = sum;
		for (i = 0; i < n; i++) if (tbl[i].len == 1) {
			q = tbl + tbl[i].to[0];
			for (j = 0; j < q->len; j++) 
				if (q->to[j] == i) { sum -= q->d[j], q->to[j] = -1; break; }
		}

		for (max = 0, i = 0; i < n; i++) {
			if (tbl[i].len == 1) continue;
			for (j = 0; j < n; j++) tbl[j].f = 0;
			a = calc(i);
			if (a > max) max = a;
		}
		printf("%d\n", 2*sum + bridge - max);
	}
	return 0;
}