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