#include <bits/stdc++.h>
int main() {
  int N, max = 0, ans = 0;
  long A[200000];
  scanf("%d", &N);
  for (int i = 0; i < N; i++) scanf("%ld", &A[i]);
  for (int i = 1; i < N; i++) {
    if (max < A[i - 1]) max = A[i - 1];
    if (max > A[i]) {
      ans += max - A[i];
      A[i] += max - A[i];
    }
  }
  printf("%d ", ans);
  return 0;
}
