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