#include <bits/stdc++.h>
int main(void) {
  int count = 0;
  int n;
  int sumN = 0;
  int i;
  int height[200100];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &height[i]);
  }
  for (i = 1; i < n; i++) {
    if (height[i - 1] > height[i]) {
      count = count + height[i - 1] - height[i];
      height[i] = height[i - 1];
    }
  }
  printf("%d\n", count);
}
