#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &res) {
  char c;
  T flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') flag = -1;
  res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
  res *= flag;
}
void gcd(long long a, long long b, long long &d, long long &x, long long &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    gcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
long long in(long long a, long long mod) {
  long long d, x, y;
  gcd(a, mod, d, x, y);
  return d == 1 ? (x + mod) % mod : -1;
}
long long qp(long long a, long long x, long long mod) {
  long long ans = 1;
  while (x) {
    if (x % 2 == 1) ans = (ans * a) % mod;
    a = (a * a) % mod;
    x /= 2;
  }
  return ans;
}
int cmp(long long a, long long b) { return a < b; }
int main() {
  int n;
  long long a[200005], m = 0, ans = 0;
  read(n);
  for (int i = 0; i < n; i++) read(a[i]);
  for (int i = 1; i < n; i++) {
    if (a[i] < m) {
      m = max(m, a[i - 1]);
      ans += m - a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
