import java.util.;
import java.lang.;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] a = new int[N];
int i = 0;
while(i<N){
a[i]=sc.nextInt();
i++;
}
System.out.println(findMax(a,N));
}
public static int findMax(int[] a,int n){
int result=0;
for(int i =1;i<n-1;i++){
int left=a[i];
for(int j=0;j<i;j++){
left=Math.max(left,a[j]);
}
int right=a[i];
for(int k=i+1;k<n;k++){
right=Math.max(right,a[k]);
}
result+=Math.min(left,right)-a[i];
}
return result;
}
}