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