#include <bits/stdc++.h>
int main(void) {
  int n = 0;
  int a = 0;
  int amin = 0;
  long int count = 0;
  scanf("%d", &n);
  while (n--) {
    scanf("%d", &a);
    if (a > amin || a == 0)
      amin = a;
    else
      count += amin - a;
    printf("n=%d, a=%d, amin=%d, count=%ld\n", n, a, amin, count);
  }
  printf("%ld\n", count);
  return 0;
}
