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)

#include <stdio.h>
#include <stdbool.h>
int memo[805][805]={0};
int next[805][805]={0};
int num[805]={0};
int num2[805]={0};
int dummy[805]={0};
bool ch[805]={0};
int n,max=0;

typedef struct{
  int now;
  int dis;
}queue;

void solve(int t);
void reset();
 queue qu[805];
 int t,co=1,nnum=0;
// queue now;

int main(void){
  int ans=0,i,j;
  while(1){
    scanf("%d\n",&n);
    if(n==0)break;
    max=0;
    ans=0;
    for(i=2;i<=n;i++){
      scanf("%d",&dummy[i]);
      next[i][num[i]]=dummy[i];
      next[dummy[i]][num[dummy[i]]]=i;
      num[i]++;
      num[dummy[i]]++;
    }
    for(i=2;i<=n;i++){
      scanf("%d",&memo[i][dummy[i]]);
      memo[dummy[i]][i]=memo[i][dummy[i]];
      if(num[i]==1||num[dummy[i]]==1)ans+=memo[i][dummy[i]];
      else ans+=memo[i][dummy[i]]*3;
    }
    /*for(i=1;i<n;i++)for(j=i+1;j<=n;j++){
	if(num[i]==1||num[j]==1)ans+=memo[i][j];
	else ans+=memo[i][j]*3;
	}*/
    for(i=1;i<=n;i++){
      if(num[i]==1)ch[i]=1;
      else ch[i]=0;
    }
    for(i=1;i<=n;i++){

      if(num[i]==1)continue;
      //solve(i);
      //reset();





      co=1;
  nnum=0;
  qu[0].dis=0;
  qu[0].now=i;
  ch[i]=1;
  while(co>nnum){
    //now.dis=qu[nnum].dis;
    //now.now=qu[nnum].now;
    if(max<qu[nnum].dis){
      // printf("%d %d\n",now.dis,now.now);
      max=qu[nnum].dis;
    }
    /*for(t=1;t<=n;t++){
      if(ch[t]==1||memo[t][qu[nnum].now]==0)continue;
      qu[co].dis=qu[nnum].dis+memo[t][qu[nnum].now];
      qu[co].now=t;
      ch[t]=1;
      co++;
      }*/
    for(t=0;t<num[qu[nnum].now];t++){
      if(ch[next[qu[nnum].now][t]]==1)continue;
      qu[co].dis=qu[nnum].dis+memo[next[qu[nnum].now][t]][qu[nnum].now];
      qu[co].now=next[qu[nnum].now][t];
      ch[next[qu[nnum].now][t]]=1;
      co++;
    }
    //for(i=1;i<=n;i++)printf("%d ",ch[i]);
    //printf("\n");
    nnum++;
    }


            for(j=1;j<=n;j++){
	if(num[j]==1)ch[j]=1;
	else ch[j]=0;
      }

      
    }
    printf("%d\n",ans-max);
    for(i=1;i<=n;i++)for(j=1;j<=n;j++)memo[i][j]=0;
    for(i=1;i<=n;i++){
      num[i]=0;
      num2[i]=0;
      dummy[i]=0;
    }
  }
  return 0;
}

void solve(int t){
  queue qu[805];
  int i,j,co=1,nnum=0;;
  queue now;
  qu[0].dis=0;
  qu[0].now=t;
  ch[t]=1;
  while(co>nnum){
    now.dis=qu[nnum].dis;
    now.now=qu[nnum].now;
    if(max<now.dis){
      // printf("%d %d\n",now.dis,now.now);
      max=now.dis;
    }
    for(i=1;i<=n;i++){
      if(ch[i]==1||memo[i][now.now]==0)continue;
      qu[co].dis=now.dis+memo[i][now.now];
      qu[co].now=i;
      ch[i]=1;
      co++;
    }
    //for(i=1;i<=n;i++)printf("%d ",ch[i]);
    //printf("\n");
    nnum++;
  }
  //printf("\n");
}
void reset(){
  int i;
  for(i=1;i<=n;i++){
    if(num[i]==1)ch[i]=1;
    else ch[i]=0;
  }
}