#include <bits/stdc++.h>
const int MAX_N = 200000;
int A[MAX_N];
int N;
void solve();
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  solve();
  return 0;
}
void solve() {
  int ans = 0, j;
  for (int i = 0; i < N - 1; i++) {
    for (j = i + 1; A[i] < A[j]; j++) {
      ans += (A[j] - A[i]);
    }
    i = j - 1;
  }
  printf("%d", ans);
}
