#include <bits/stdc++.h>
using namespace std;
bool prime[1000001];
void seive() {
  memset(prime, true, sizeof(prime));
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i * i <= 1000001; i++) {
    if (prime[i]) {
      for (int j = i * i; j <= 1000001; j += i) {
        prime[j] = false;
      }
    }
  }
}
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int c = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] <= a[i - 1]) {
      a[i] += (a[i - 1] - a[i]);
      c += (a[i - 1] - a[i]);
    }
  }
  cout << c << endl;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  solve();
  return 0;
}
