#include <bits/stdc++.h>
int main(void) {
  int n;
  int i, j;
  scanf("%d", &n);
  long long A[n];
  long long X = 0;
  int max;
  j = n;
  int k = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &A[i]);
  }
  while (j > 0) {
    max = 0;
    k = 0;
    for (i = 0; i < j; i++) {
      if (max < A[i]) {
        max = A[i];
        k = i;
      }
    }
    for (i = k; i < j; i++) {
      X += max - A[i];
    }
    j = k;
  }
  printf("%lld", X);
  return 0;
}
