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