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