import Control.Applicative
import Control.Monad
import Control.Monad.ST
import Data.Array
import Data.Array.ST
import Data.List

getInts :: IO [Int]
getInts = map read . words <$> getLine

createTree :: Int -> Array Int Bool -> [((Int, Int), Int)] -> Array Int [(Int, Int)]
createTree n nl es = runSTArray $ do
    g <- newArray (0, n) [] :: ST s (STArray s Int [(Int, Int)])
    forM_ es $ \((i, j), w) -> when (nl!i && nl!j) $ do
      readArray g i >>= writeArray g i . ((j, w):)
      readArray g j >>= writeArray g j . ((i, w):)
    writeArray g 0 $ map (fmap (const 0)) $ filter snd $ assocs nl
    return g

treeEdges :: Int -> Array Int Bool -> [Int] -> [Int] -> [((Int, Int), Int)]
treeEdges n nl ps ds = filter (\((i, j), _) -> nl!i && nl!j) $ zip (zip [2..] ps) ds

notLeaf :: Int -> [Int] -> Array Int Bool    
notLeaf n ps = runSTArray $ do
    a <- newListArray (1, n) (0:[1,1..]) :: ST s (STArray s Int Int)
    forM_ ps $ \i -> readArray a i >>= writeArray a i . (+1)
    mapArray (>=2) a 

dfs g x p = maximum (0: [w + dfs g y x | (y, w) <- g!x, y /= p])

calc :: Int -> [Int] -> [Int] -> Int
calc n ps ds = sum ds + 2 * sum (map snd te) - dfs tree 0 0
  where
    nl = notLeaf n ps
    tree = createTree n nl te
    te = treeEdges n nl ps ds

main = readLn >>= \n -> when (n/=0) $ calc n <$> getInts <*> getInts >>= print >> main
