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