#include <bits/stdc++.h>
using namespace std;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
inline void setPrecision(int n) { cout.precision(n); }
long long int INF = 1e10;
int MOD = 1e9 + 7;
int main() {
  fastio();
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i) cin >> a[i];
  long long int mv = 0;
  for (int i = 1; i < n; ++i) {
    if (a[i - 1] > a[i]) {
      mv += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  cout << mv;
  return 0;
}
