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