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

Measured memory_bytes: 4856