#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const double PI = acos(-1);
const int mod = 1e9 + 7;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
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; }
int main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  int min = 0;
  for (int i = 0; i < (n); ++i) {
    if (a[i] > a[i + 1]) {
      min += a[i] - a[i + 1];
      a[i + 1] += a[i] - a[i + 1];
    } else if (a[i] <= a[i + 1]) {
      min += 0;
    }
  }
  cout << min << endl;
  return 0;
}
