#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
int two(int n) {
  ll ans = 1;
  for (int i = 0; i < (n); ++i) {
    ans *= 2;
  }
  return ans;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  vector<ll> a(2000000);
  cin >> n;
  for (int i = 0; i < (n); ++i) cin >> a[i];
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] >= a[j]) {
        sum += a[i] - a[j];
        break;
      }
    }
  }
  cout << sum << endl;
}
