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