#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  ll n, Max = 0, ans = 0, n_ = 0;
  cin >> n;
  ll k, j = n - 1;
  vector<ll> a(n);
  for (ll i = 0; i < (n); i++) {
    cin >> a[i];
    if (Max < a[i]) {
      Max = a[i];
      k = i;
    }
    if (a[0] == a[i]) n_++;
  }
  if (n_ == n) {
    cout << 0 << endl;
    return 0;
  }
  while (j != -1) {
    for (; j != k; j--) {
      ans += a[k] - a[j];
    }
    j = k - 1;
    if (j <= 0) break;
    Max = 0;
    ll l = k;
    for (ll i = 0; i < (l); i++) {
      if (Max < a[i]) {
        Max = a[i];
        k = i;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
