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