#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const long long N = 2e6 + 5;
const long long INF = 1e18L;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << ": " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
long long ar[N];
long long dp[N];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long T = 1;
  while (T--) {
    long long n;
    cin >> n;
    for (long long i = 0; i < n; i++) {
      cin >> ar[i];
    }
    long long i = n - 1;
    long long ans = 0;
    while (i >= 0) {
      long long d = INT_MIN;
      long long j = i;
      while (i - 1 >= 0 && ar[i - 1] >= ar[i]) i--;
      while (j >= i) ans += ar[i] - ar[j], j--;
      i--;
    }
    cout << ans;
  }
  return 0;
}
