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