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