#include <bits/stdc++.h>
int main() {
  int n;
  scanf("%d", &n);
  int sum = 0;
  int x, pos = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    if (pos > x) {
      sum = sum + (pos - x);
    }
    if (pos <= x) {
      pos = x;
    }
  }
  printf("%d \n", sum);
}
