#include <bits/stdc++.h>
int main() {
  int n;
  scanf("%d\n", &n);
  int a[n];
  scanf("%d\n", &a[0]);
  int n2 = n - 1;
  int i = 1;
  int x, sum;
  while (n2--) {
    scanf("%d\n", &a[i++]);
    x = a[i] - a[i + 1];
    if (x > 0) sum += x;
  }
  printf("%d\n", sum);
  return 0;
}
