#include <bits/stdc++.h>
int main(void) {
  int n, i, j, count = 0;
  int A[200001];
  setbuf(stdout, NULL);
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  for (i = 0; i < n - 1; i++) {
    for (j = 1; i + j < n; j++) {
      if (A[i] <= A[i + j]) {
        break;
      } else if (A[i] > A[i + j]) {
        count += A[i] - A[i + j];
      }
    }
  }
  printf("%d", count);
  return 0;
}
