#include <bits/stdc++.h>
using namespace std;
long long int const mod = 1e9 + 7;
long long int const modl = 1e18;
long long int gcd(long long int x, long long int y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
long long int fib(int n) {
  long long int a, b, c, i;
  a = 0, b = 1;
  if (n == 0) return a;
  for (i = 2; i <= n; i++) {
    c = a + b;
    a = b;
    b = c;
  }
  return b;
}
bool isprime(long long int n) {
  if (n <= 1)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;
  else {
    int i;
    for (i = 3; i * i <= n; i++) {
      if (n % i == 0) return false;
    }
    return true;
  }
}
void readwrite() {}
vector<long long int> prime(100000 + 7, 1);
void buildprime(long long int n) {
  long long int i, j;
  prime[0] = prime[1] = 0;
  for (i = 2; i * i <= n; i++) {
    if (prime[i]) {
      for (j = i * i; j <= n; j += i) prime[j] = 0;
    }
  }
}
long long int dp[1005][1005];
long long int comb[1005][1005];
long long int ncr(long long int n, long long int r) {
  if (n == r) return 1;
  if (r == 0) return 1;
  if (r == 1) return n;
  if (dp[n][r] != 0) return dp[n][r];
  return dp[n][r] = (ncr(n - 1, r) % mod + ncr(n - 1, r - 1) % mod) % mod;
}
void ncrm() {
  comb[0][0] = 1;
  for (int i = 1; i < 1001; i++) {
    comb[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      comb[i][j] = (comb[i - 1][j] % mod + comb[i - 1][j - 1] % mod) % mod;
    }
  }
}
long long int binpowm(long long int a, long long int b) {
  long long int res = 1;
  a %= mod;
  while (b) {
    if (b & 1) res = ((res % mod) * (a % mod)) % mod;
    a = ((a % mod) * (a % mod)) % mod;
    b >>= 1;
  }
  return res % mod;
}
long long int fact(long long int n) {
  long long int i, res = 1;
  for (i = 1; i <= n; i++) res = ((res % mod) * (i % mod)) % mod;
  return res % mod;
}
void solve() {
  int n, i;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i < n; i++) cin >> a[i];
  int curr = a[0], ans = 0;
  for (i = 1; i < n; i++) {
    if (a[i] >= curr) {
      curr = a[i];
    } else {
      ans += (curr - a[i]);
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.setf(std::ios::fixed, std::ios::floatfield);
  cout.precision(9);
  ;
  int t = 1;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
