#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using PLL = pair<long long, long long>;
const int INF = numeric_limits<int>::max();
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
long long mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0) res += m;
  return res;
}
long long gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
long long lcm(ll a, ll b) { return a / gcd(a, b) * b; }
bool is_prime(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return n != 1;
}
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  int ans = 0;
  for (int i = 0; i < (n); i++) cin >> a[i];
  int hmin = a[0];
  for (int i = 1; i < (n); i++) {
    if (a[i] < hmin) {
      ans += hmin - a[i];
    }
    if (a[i] > hmin) {
      hmin = a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
