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