#include <bits/stdc++.h>
int main() {
  long int n, k, i, x;
  scanf("%d", &n);
  long int a[n], count = 0;
  for (i = 0; i < n; i++) {
    if (i >= 1) {
      if (a[i] < a[i - 1]) {
        x = a[i - 1] - a[i];
        count += x;
        a[i] = a[i] + x;
      }
    }
  }
  printf("%ld", count);
}
