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