#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int *a;
  int height = 0;
  int max = 0;
  cin >> n;
  a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (max > a[i]) {
      height += max - a[i];
    } else {
      max = a[i];
    }
  }
  cout << height;
  delete[] a;
  return 0;
}
