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