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