human
Write me a Python code to print the minimum time units required to remove all the bridges given the following:
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...
#!/us
assistant
#Here is Python code to print the minimum time units required to remove all the bridges.
r/bin/env python

from collections import deque
import itertools as it
import sys

sys.setrecursionlimit(1000000)

while True:
    N = input()
    if N == 0:
        break
    p = map(int, raw_input().split())
    d = map(int, raw_input().split())
    E = [[] for i in range(N)]
    leaf = [False for i in range(N)]
    for i in range(N - 1):
        u = i + 1
        v = p[i] - 1
        cost = d[i]
        E[u].append((v, cost))
        E[v].append((u, cost))
    
    for i in range(N):
        leaf[i] = (len(E[i]) < 2)
    
    def func(num, pre, dist):
        if leaf[num]:
            return (0, 0)
        ret = (dist, num)
        for e in E[num]:
            if e[0] != pre:
                hoge = func(e[0], num, dist + e[1])
                if ret[0] < hoge[0]:
                    ret = hoge
        return ret

    for i in range(N):
        if not leaf[i]:
            ret = func(i, -1, 0)
            ret = func(ret[1], -1, 0)
            break
    ans = -2 * ret[0]
    for i in range(N):
        for e in E[i]:
            if leaf[i] or leaf[e[0]]:
                ans += e[1]
            else:
                ans += e[1] * 3
    print ans / 2