#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<long long> arr(n);
  for (long long i = 0; i < n; i++) cin >> arr[i];
  long long maxi = arr[0];
  long long ans = 0;
  for (long long i = 1; i < n; i++) {
    if (arr[i] <= maxi) ans += maxi - arr[i];
    maxi = max(maxi, arr[i]);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  t = 1;
  while (t--) solve();
}
