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