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