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