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