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