#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long infll = 1e18 + 9;
const int infint = 1e9 + 7;
const int MAXN = 1e5 + 7;
const double eps = 1e-9;
const long double pi = 3.1415926535897932384626433832795;
const long double e = 2.71828182845904523536028747135266;
inline long long read() {
  long long kr = 1, xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls)) {
    if (!(ls ^ 45)) kr = -1;
    ls = getchar();
  }
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs * kr;
}
inline unsigned long long readull() {
  unsigned long long xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls)) ls = getchar();
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs;
}
inline void writeull(unsigned long long x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10) writeull(x / 10);
  putchar('0' + x % 10);
}
inline void write(long long x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10) write(x / 10);
  putchar('0' + x % 10);
}
char a[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    int n = read();
    for (int i = 1; i <= n; i++) {
      a[i] = read();
    }
    long long ans = 0;
    for (int i = 2; i <= n; i++) {
      if (a[i] < a[i - 1]) {
        ans += (a[i - 1] - a[i]);
        a[i] = a[i - 1];
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
