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