#include <bits/stdc++.h>
int main() {
  int n, i, max;
  unsigned int base = 0;
  int a[2000000];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  max = a[0];
  for (i = 0; i < n; i++) {
    if (max < a[i]) {
      max = a[i];
    }
    base += max - a[i];
  }
  printf("%u", base);
}
