#include <bits/stdc++.h>
int up_comp(const void *a, const void *b) { return *(int *)a - *(int *)b; }
int down_comp(const void *a, const void *b) { return *(int *)b - *(int *)a; }
int int_sort(const void *a, const void *b) {
  return strcmp((char *)a, (char *)b);
}
int count_digit(int a) {
  int i = 0;
  while (a != 0) {
    a = a / 10;
    i++;
  }
  return i;
}
int euclid(int a, int b) {
  int temp;
  if (a < b) {
    temp = a;
    a = b;
    b = temp;
  }
  if (b < 1) return -1;
  if (a % b == 0) return b;
  return euclid(b, a % b);
}
int main(void) {
  int n, a[200001];
  long long int sum = 0;
  int amax = INT_MIN, mem;
  scanf("%d", &n);
  for (int i = 0; i < (n); ++i) {
    scanf("%d", &a[i]);
    if (amax < a[i]) {
      amax = a[i];
      mem = i;
    }
  }
  for (int i = 0; i < mem - 1; i++) {
    if (a[i] > a[i + 1]) {
      sum += (a[i] - a[i + 1]);
    }
  }
  for (int i = mem + 1; i < n; i++) {
    if (a[mem] > a[i]) {
      sum += (a[mem] - a[i]);
    }
  }
  printf("%lld", sum);
  return 0;
}
