#include <bits/stdc++.h>
using namespace std;
long long max(long long b, long long c) { return (b > c) ? b : c; }
int main() {
  long long n, s, count = 0;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] > a[i + 1]) {
      s = max(0LL, a[i] - a[i + 1]);
      count = count + s;
      a[i + 1] = a[i + 1] + s;
    } else {
      continue;
    }
  }
  cout << count;
}
