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