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 <stdlib.h>

#define MAXN 800

//??????????????¶
//??¨????????????
//http://www.cse.kyoto-su.ac.jp/~hiraishi/ICPC/ICPC2014/Domestic/ICPC2014_DomE/ICPC2014_DomE.pdf

//??¶????§???????
typedef struct {
    int ne; //???????????£?????????????????° num_edge
    int eids[MAXN]; //???????????£????????????????????\?????? edge_ids
    int rm; //1?????¨???????????????????????????
} Node;

//???????§???????
typedef struct {
    int nid1, nid2; //???????????£???????????¶????????\???????????? node_id
    int len; //????????????
    int rm; //1?????¨???????????????????????????
} Edge;

//??????????????????????????¶??¨???????????\???????????????
Node nodes[MAXN]; 
Edge edges[MAXN - 1];
int N = 0, C1, C2, C3;

//nid????????¶???????????????????????? node_id
Node* nd(int nid)
{
    return &nodes[nid - 1];
}

//eid????????????????????????????????? edge_id
Edge* ed(int eid)
{
    return &edges[eid - 1];
}

//???????????°??¨
void DebugPrint()
{
    int i, j;
    for(i = 1; i <= N; i++)
    {
        printf("\n%d????????¶\n????????£?????????????????°: %d\n", i, nd(i)->ne);
        printf("????????£??????????????????: ");
        for(j = 0; j < nd(i)->ne; j++)
        {
            printf("%d,",nd(i)->eids[j]);
        }
        printf("\nRemoved: %s\n\n", (nd(i)->rm ? "true" : "false"));
    }
    for(i = 1; i <= N - 1; i++)
    {
        printf("%d?????????\n????????£???????????¶??????: %d,%d\n", i, ed(i)->nid1, ed(i)->nid2);
        printf("????????????: %d\n", ed(i)->len);
        printf("Removed: %s\n\n", (ed(i)->rm ? "true" : "false"));
    }

    printf("C1: %d\nC2: %d\nC3: %d\n", C1, C2, C3);
}

//pnid: ??´????????¶???nid pre_nid, cnid: ?????¨?????¶???nid current_nid, d: ????????§????????¢???????¨?
int Search(int pnid, int cnid, int d)
{
    int i, eid, x, maxd = d; //?????§?????¢???????????????????????§????????¢???????¨?

    for(i = 0; i < nd(cnid)->ne; i++)
    {
        eid = nd(cnid)->eids[i];
        if(ed(eid)->rm) continue; //???????????????????????????
        if(ed(eid)->nid1 == pnid || ed(eid)->nid2 == pnid) continue; //??´????????¶??????????????????
        x = (ed(eid)->nid1 != cnid ? Search(cnid, ed(eid)->nid1, d + ed(eid)->len) : Search(cnid, ed(eid)->nid2, d + ed(eid)->len)); //???????????????????????°?????\??????
        if(x > maxd) maxd = x; //?????????????????§????????§?????¢???????????§????????¨???
    }

    return maxd; //?????§?????¢?????????
}

//???????????¨??¢??°
void Init()
{
    int i, j;

    C1 = 0;
    C2 = 0;
    C3 = 0;

    for(i = 1; i <= MAXN; i++)
    {
        nd(i)->ne = 0;
        for(j = 0; j < MAXN; j++)
        {
            nd(i)->eids[j] = 0;
        }
        nd(i)->rm = 0;
    }

    for(i = 1; i <= MAXN - 1; i++)
    {
        ed(i)->nid1 = 0;
        ed(i)->nid2 = 0;
        ed(i)->rm = 0;
        ed(i)->len = 0;
    }
}

int main()
{
    int i, x;
    int p; //??\????????¶??????
    int d; //??\??????????????????

    while(1)
    {
        //??¶?????°?????\???
        scanf("%d", &N); 

        //0?????\??????????????¨???????????????????????????
        if(N == 0) break;

        //???????????????
        Init();

        //i????????¶????????????
        //i-1?????????????????????
        for(i = 2; i <= N; i++)
        {
            //i????????¶??????????????£???????????¶???????????????
            scanf("%d", &p); 
            //i-1???????????????????????£???????????¶???????????????
            ed(i - 1)->nid1 = p;
            ed(i - 1)->nid2 = i;
            //i???,p????????¶??????????????£???????????????????????????????????????????????£?????????????????°????¢???????
            nd(p)->eids[nd(p)->ne++] = i - 1;
            nd(i)->eids[nd(i)->ne++] = i - 1;     
        }

        //i?????????????????????
        for(i = 1; i <= N - 1; i++)
        {
            //i???????????????????????????
            scanf("%d", &(ed(i)->len));
        }

        //i????????¶????????????
        for(i = 1; i <= N; i++)
        {
            //??????????????£?????????????????°??????????????¨???
            if(nd(i)->ne == 1)
            {
                //??¶???????????????
                nd(i)->rm = 1;
                //??????????????????
                ed(nd(i)->eids[0])->rm = 1;
                //??????????????????????????????C1?????????????????????
                C1 += ed(nd(i)->eids[0])->len;
            }
        }
        //i?????????????????????
        for(i = 1; i < N - 1; i++)
        {
            //??????????????????????????????????????????
            if(ed(i)->rm) continue;

            //??????????????????????????????????????????C2?????????????????????
            C2 += ed(i)->len;
        }

        //i???????????¶???????????????????????§???????????¶?????§????????¢????????????C3?????£??\
        for(i = 1; i <= N; i++)
        {
            if(nd(i)->rm) continue; //?????????????????¶?????????
            x = Search(-1, i, 0);
            if(x > C3) C3 = x;
        }

        //DebugPrint();

        //(????????????????????§???????????????????????????)+(???????????£????????£?????????????????????????????????????????????)-(????????????????????????????????????)
        printf("%d\n", C1 + 3 * C2 - C3); 
    }

    return 0;
}