<?php                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                               
function fget_array() {                                                                                                                                                                                                                                                                                                        
    return explode(' ', trim(fgets(STDIN)));                                                                                                                                                                                                                                                                                   
}                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                               
$n = fget_array()[0];                                                                                                                                                                                                                                                                                                          
$a = fget_array();                                                                                                                                                                                                                                                                                                             
$b = fget_array();                                                                                                                                                                                                                                                                                                             
$c = fget_array();                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                               
$sum = 0;                                                                                                                                                                                                                                                                                                                      
$prev = 99;                                                                                                                                                                                                                                                                                                                    
for ($i = 0; $i < $n; $i++) {                                                                                                                                                                                                                                                                                                  
    $j = $a[$i] - 1;                                                                                                                                                                                                                                                                                                           
    $sum += $b[$j];                                                                                                                                                                                                                                                                                                            
    if ($j == ($prev + 1)) {                                                                                                                                                                                                                                                                                                   
        $sum += $c[$j - 1];                                                                                                                                                                                                                                                                                                    
    }                                                                                                                                                                                                                                                                                                                          
    $prev = $j;                                                                                                                                                                                                                                                                                                                
}                                                                                                                                                                                                                                                                                                                              
echo $sum;                                                                                                                                                                                                                                                                                                                     
?> 

Measured memory_bytes: 4728