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