#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; };
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; };
long long mod(long long a, long long b) { return (a + b - 1) / b; };
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
  std::fill((T*)array, (T*)(array + N), val);
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
signed main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  long long a[n];
  for (long long(i) = 0; i < (n); i++) cin >> a[i];
  long long ans = 0;
  for (long long(i) = (1); (i) < (n); (i)++) {
    long long now = max(0LL, a[i] - a[i - 1]);
    ans += now;
    a[i] += now;
  }
  cout << ans << "\n";
  return 0;
}
