#include <bits/stdc++.h>
int a[20000 + 10];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i != n; i++) scanf("%d", &a[i]);
  int cnt = 0;
  for (int i = 1; i != n; i++)
    if (a[i - 1] > a[i]) {
      cnt += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  printf("%d", cnt);
}
