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