#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-') sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}
const int INF = 2e9;
const int MX = 100005;
const int MOD = 1000000007;
const double PI = acos(-1.0);
vector<int> sieve(100001);
long long int power(long long int a, long long int b) {
  a %= MOD;
  long long int res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
bool isprime(int n) {
  if (n == 1) return 0;
  int i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
void SIEVE() {
  int i, j;
  for (i = 0; i <= 100000; i++) sieve[i] = i;
  for (i = 2; i * i <= 100000; i++) {
    if (sieve[i] != i) continue;
    for (j = i * i; j <= 100000; j = j + i) {
      if (sieve[j] == j) sieve[j] = i;
    }
  }
}
int main() {
  int n;
  cin >> n;
  int a[n], i;
  int ans = 0, mx = 0, ans1 = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
    ans += max(0, mx - a[i]);
  }
  cout << ans;
}
