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