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