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: Memory Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1000//?§????????????????????????????????±???????
#define MAX 100000//?????§???MAX??¨????????????
typedef struct List{//??¶??¨????????????????????????????´????????§???????
    int count;
    int listps[SIZE];
    int listds[SIZE];
}List;

 int n;//??¶?????°????¨?????????????????????????

 int **dp;//??¶???????§??????????????????\??????????????????????????????0???????????????????????????2???????????§????????????????????\??????1?????????2????????????2???????????§????????????????????\??????
 int *edge;//??¶????????§??????????????§?????????????????\?????????????????????
List *node;//??¶?????????????????\??????



void add(List* node,int* num);
void func(int c, int p);
int min(int n1,int n2);
int main(void){
    int ans[SIZE]={};
    int k=0;
    int i,r;
    int num;//????°??????????????¨????????????????????????°
    while(1){
        
        scanf("%d",&n);//??¶?????°?????\?????????
        if(n == 0)//0??¨??\????????????????¨??????????
            break;
        
        dp = (int**)malloc(sizeof(int*)*2);
        for(i=0;i<2;i++)
            dp[i]=(int*)malloc(sizeof(int)*n);//?????¢????¢????
        
        node =(List*)malloc(sizeof(List)*n);
        for(i=0;i<n;i++)
            node[i].count=0;
        
        edge = (int*)malloc(sizeof(int)*n);//?????¢????¢????
        
        
        
        
        int *ps =(int*)malloc(sizeof(int)*(n-1));//???????????¶?????????????????£????????????????¨?????????????????????????
        int *ds = (int*)malloc(sizeof(int)*(n-1));//???????????????????????????????¨?????????????????????????
        
        for( i = 0; i < n - 1; i++){
            scanf("%d",&ps[i]);//??????????????\???
            ps[i]-=1;
        }
        for( i = 0; i < n - 1; i++)
            scanf("%d",&ds[i]);//??????????????\???
        
        
        for(i = 0; i < n - 1; i++){//node????????????????¨????????????????
            int l[]={ps[i],ds[i]};//??¶??¨??????????????????????????????????????§??????
            add(&node[i+1],l);//????????????????´?
            
            int m[]={i+1,ds[i]};
            add(&node[ps[i]],m);//???????????????????????????????´?????????????
            
        }
        
        num = MAX;//?????????MAX??§???????????????
        for( r = 0; r < n; r++){
            func(r, r);//?????????????¨????
            num = min(num, dp[0][r]);//??????????¨??????????????????¨??????????????§????????????????????????
        }
        ans[k]=num;//???????????????
        k++;
    }
    for(i=0;i<k;i++)
        printf("%d\n",ans[i]);//???????????????
    
    return 0;
}

void func(int s, int p){//s??????p?????§???????°??????????????¨?????????????????????¢??°
        
    dp[1][s] = 0;//???????????????????????§???????????????????????¨??????
    edge[s] = 1;//s????????§????????¨??????
    int i;
    for(i=0;i<node[s].count;i++){
        int to = node[s].listps[i];//to?????¶???,cost?????????????????\??????
        int cost = node[s].listds[i];
        if(to == p)//to??¨p??????????????£?????????????????????
        continue;
        
        func(to, s);//?????????to??????s?????§????????????????¨??????????
       
        dp[1][s] += dp[1][to];//dp[1][s]???dp[1][to]????¶????????????????
        if(edge[to]!=1)//to????????§???????????£??????
        dp[1][s] += 3 * cost; //???????§??????¨??´?£?????????????????¨????
        
        else//?????§?????£??????
        dp[1][s] += cost;//??´?£????????????????????¨????
        
      edge[s] = 0;//s????????§???????????????
    }
    dp[0][s] = dp[1][s];//ds[1][s]?????????????°?????????????????????£?????????
    
    //???????????¶?????????????????????????±???????
    for(i=0;i<node[s].count;i++){
        int to = node[s].listps[i];
        int cost = node[s].listds[i];
        if(to == p || edge[to]==1)//to?????????p??¨????????´??????,???????????????
        continue;
        
        int tmp = dp[1][s]; //???????????????????????§????????????????¨????????????????
        
        tmp -= dp[1][to]; //?????????to??????????????????????????????
        tmp += dp[0][to]; //to????????????????????£???????????¶???????§??????£????????¨?????????
        tmp -= cost; //???????????????????????????????????§???cost??????????????????
        
        dp[0][s] = min(dp[0][s], tmp);//min??¨d[0][s]????°?????????????dp[0][s]??¨??????
    }
}

int min(int n1,int n2){//?°?????????????????????¢??°
    if(n1>n2)
        return n2;
    else
        return n1;
}

void add(List* node,int* num){//?§??????????????????????????´??????????????????¢??°
    
    
    node->listps[node->count]=num[0];
    node->listds[node->count]=num[1];
    node->count+=1;
}