#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 min = A[0];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (min <= A[i])
      min = A[i];
    else
      cnt += min - A[i];
  }
  printf("%d\n", cnt);
  return 0;
}
