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