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