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