#include <stdio.h>
int main(void){
  int N,i,s=0,max=0;
  int A[200001]
  setbuf(stdout,NULL);
  scanf("%d",&N);
  for(i=0;i<N;i++){
    scanf("%d",&A[i]);
    if(A[i]<max)s+=max-A[i];
    else max=A[i];
  }
  printf("%d",s);
  return 0;
}