#include <bits/stdc++.h>
const int MAX_N = 200005;
int as[MAX_N];
int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", as + i);
  }
  int prev = as[0];
  int count = 0;
  for (int i = 1; i < N; ++i) {
    if (as[i] < prev) {
      count += prev - as[i];
      as[i] = prev;
    }
    prev = as[i];
  }
  printf("%d\n", count);
}
