#include <bits/stdc++.h>
using namespace std;
struct pair_hash {
  template <class T1, class T2>
  size_t operator()(const pair<T1, T2> &p) const {
    auto h1 = hash<T1>{}(p.first);
    auto h2 = hash<T2>{}(p.second);
    return h1 ^ h2;
  }
};
using Vote = pair<long long int, long long int>;
using Unordered_map = unordered_map<Vote, int, pair_hash>;
const int mod = 998244353;
const int mod2 = 1000000007;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int count = 0;
  int n;
  cin >> n;
  long long int a[n];
  for (long long int i = 0; i < n; i++) cin >> a[i];
  ;
  long long int maxm = 0;
  for (long long int i = 0; i < n; i++) {
    maxm = max(maxm, a[i]);
    count += maxm - a[i];
  }
  cout << count << '\n';
}
