#include <bits/stdc++.h>
int main(int argc, const char* argv[]) {
  int n;
  scanf("%d", &n);
  int a[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (i >= 1) {
      if (a[i - 1] > a[i]) {
        sum += a[i - 1] - a[i];
        a[i] = a[i - 1];
      }
    }
  }
  printf("%d\n", sum);
  return 0;
}
