#include <bits/stdc++.h>
using namespace std;
void printarray(int *a, int n) {
  for (int i = 0; i < n; i++) cout << a[i] << " ";
}
void scanarray(int *a, int n) {
  for (int i = 0; i < n; i++) cin >> a[i];
}
int main() {
  int n;
  cin >> n;
  int a[200000];
  scanarray(a, n);
  int maxx = a[0];
  int s = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < maxx)
      s += (maxx - a[i]);
    else
      maxx = a[i];
  }
  cout << s;
}
