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