#include <bits/stdc++.h>
using namespace std;
const char NL = '\n';
const char SP = ' ';
int main() {
  ios_base::sync_with_stdio(false);
  int n, c = 0;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int min = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] >= min) {
      min = a[i];
    } else {
      c += min - a[i];
    }
  }
  cout << c << NL;
  return 0;
}
