#include <bits/stdc++.h>
int main() {
  int N;
  scanf("%d", &N);
  int A[N];
  for (int Ai = 0; Ai < N; Ai++) {
    scanf("%d", &A[Ai]);
  }
  int total_hight = 0;
  int tmp = A[0];
  for (int Ai = 1; Ai < N; ++Ai) {
    if (tmp > A[Ai]) {
      total_hight += tmp - A[Ai];
      total_hight = A[Ai];
    }
  }
  printf("%d", total_hight);
  return 0;
}
