#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int sum = 0, k, i = 0, n;
  cin >> n;
  long long int a[n];
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = 0; i < n; i++) {
    if (a[i] > a[i + 1]) {
      sum += a[i] - a[i + 1];
      a[i + 1] += a[i] - a[i + 1];
    }
  }
  cout << sum << endl;
}
