https://www.luogu.org/problemnew/show/P1318
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
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 B[]=new int [n];
int C[]=new int [n];
for(int i=0;i<n;i++){
A[i]=Sc.nextInt();
}
B[0]=A[0];
for(int i=1;i<n;i++){
if(B[i-1]>A[i])B[i]=B[i-1];
else B[i]=A[i];
}
C[n-1]=A[n-1];
for(int i=n-2;i>=0;i--){
if(C[i+1]>A[i])C[i]=C[i+1];
else C[i]=A[i];
}
int ans=0;
for(int i=0;i<n;i++){
ans+=(Math.min(B[i], C[i])-A[i]);
//System.out.println(B[i]+" "+C[i]);
}
System.out.println(ans);
}}