#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;
  for (int i = 0; i < N - 2; i++) {
    if (A[i] > A[i + 1]) {
      ans += (A[i] - A[i + 1]);
      A[i + 1] = A[i];
    }
  }
  printf("%d", ans);
}
