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