#include<stdio.h>
#include <stdlib.h>
#include<string.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;
  while(n2--){
    scanf("%d\n", &a[i++]);
    x = a[i] - a[i+1];
    if(x > 0)
      sum += x;
  }
  printf("%d\n", sum);
  return 0;
}
