#include <bits/stdc++.h>
void swap(int *x, int *y) {
  int temp;
  temp = *x;
  *x = *y;
  *y = temp;
}
int gcd(int a, int b) {
  if (a < b) {
    swap(&a, &b);
  }
  if (b == 0) {
    return a;
  }
  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = (a % b);
  }
  return b;
}
int dn(const void *a, const void *b) { return *(int *)b - *(int *)a; }
int modpow(int a, int n, int mod) {
  int ans = 1;
  while (n > 0) {
    if (n & 1) {
      ans = ans * a % mod;
    }
    a = a * a % mod;
    n /= 2;
  }
  return ans;
}
int max(int a, int b) {
  if (a < b) {
    return b;
  } else {
    return a;
  }
}
int min(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
void chmax(int *a, int b) {
  if (*a < b) {
    *a = b;
  }
  return;
}
void chmin(int *a, int b) {
  if (*a > b) {
    *a = b;
  }
  return;
}
int main(void) {
  int n;
  scanf("%d", &n);
  long int a[n];
  long int d[n];
  for (int i = 0; i < n; i++) {
    scanf("%ld", &a[i]);
  }
  d[0] = 0;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] <= a[i]) {
      d[i] = 0;
    } else {
      d[i] = a[i - 1] - a[i];
      a[i] += d[i];
    }
    ans += d[i];
  }
  printf("%d\n", ans);
  return 0;
}
