#include <bits/stdc++.h>
int main() {
  long long int a[2 * 100000 + 10];
  int i = 0, n, max = -1;
  long long int cou = 0;
  scanf("%d%d", &n);
  for (i; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (i = 0; i < n; i++) {
    max = max > a[i] ? max : a[i];
    cou += max - a[i];
  }
  printf("%lld", cou);
}
