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