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