<?php
fscanf(STDIN, "%s", $n);

$list = explode(" ", fgets(STDIN));

// echo print_r($list);
$answer = 0;

for($i = 0; $i < count($list); $i++){
    if($i === 0 || $list[$i] >= $list[$i-1]){
        continue;
    }
    else{
        while($list[$i] < $list[$i-1]){
            $list[$i] ++;
            $answer ++;
        }
    }
}

echo $answer;

?>
