#include <bits/stdc++.h>
int main(void) {
  int N, A[2 * 10 * 10 * 10 * 10 * 10];
  int R = 0, T = 0, count = 0;
  scanf("%d", &N);
  if ((1 > N) || (N > 2 * 10 * 10 * 10 * 10 * 10)) {
    exit(1);
  }
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
    if ((1 > A[i]) || (A[i] > 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) {
      exit(1);
    }
  }
  for (int j = 0; j < N; j++) {
    if (R <= A[j]) {
      R = A[j];
    } else {
      T = R - A[j];
      A[j] = A[j] + T;
      count = count + T;
      T = 0;
    }
  }
  printf("%d\n", count);
  return 0;
}
