#include <bits/stdc++.h>
const int MAXN = 2e5 + 5;
int N;
int num[MAXN];
int main() {
  scanf("%d", &N);
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%d", &num[i]);
    if (num[i] < num[i - 1]) {
      ans = ans + (long long)num[i - 1] - num[i];
      num[i - 1] = num[i];
    }
  }
  printf("%lld", ans);
  return 0;
}
