#include <bits/stdc++.h>
int a[100001];
int main() {
  long long i, n, m, k;
  while (scanf("%I64d", &n) == 1) {
    long long m = 0;
    for (i = 0; i < n; i++) scanf("%I64d", &a[i]);
    for (i = 0; i < n - 1; i++) {
      if (a[i] > a[i + 1]) m += (a[i] - a[i + 1]);
    }
    printf("%I64d\n", m);
  }
  return 0;
}
