#include <bits/stdc++.h>
int main() {
  int N;
  if (scanf("%d", &N) == 1)
    ;
  int A[N];
  for (int i = 0; i < N; i++) {
    if (scanf("%d ", &A[i]) == 1)
      ;
  }
  int tall[N];
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      tall[i] = A[i];
    } else {
      if (tall[i - 1] < A[i]) {
        tall[i] = A[i];
      } else {
        tall[i] = tall[i - 1];
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < N; i++) {
    sum += tall[i] - A[i];
  }
  printf("%d\n", sum);
}
