#include <bits/stdc++.h>
int N_in;
int X_in;
int out;
int main(int argc, char* argv[]) {
  int i, j;
  int stand = 0;
  int high = 0;
  int now = 0;
  int before = 0;
  scanf("%d", &N_in);
  for (i = 1; i <= N_in; i++) {
    before = now;
    scanf("%d", &now);
    if (before > now) {
      stand += before - now;
      now = before;
    }
  }
  printf("%d", stand);
  return 0;
}
