#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool solve(ll n) {
  if (n == 0 || n == 9) return true;
  if (n < 9) return false;
  return solve((ll)(n >> 3) - (ll)(n & 7));
}
int main() {
  ll n;
  cin >> n;
  vector<ll> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  ll currmx = v[0];
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    currmx = max({ans, v[i], currmx});
    ans += (currmx - v[i]);
  }
  cout << ans << endl;
  return 0;
}
