#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 200001;
int main(void) {
  ios::sync_with_stdio(false);
  long long n, i, sum = 0, temp;
  long long a[MAXN];
  cin >> n;
  for (i = 0; i < n; i++) cin >> a[i];
  i = 0;
  while (i < n) {
    while (i < n - 1 && a[i] <= a[i + 1]) i++;
    if (i < n - 1) {
      i++;
      temp = a[i - 1] - a[i];
      sum = sum + temp;
      a[i] = a[i - 1];
    }
    if (i == n - 1) {
      temp = a[i - 1] - a[i];
      sum = sum + temp;
      break;
    }
  }
  cout << sum;
  return 0;
}
