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