#include <bits/stdc++.h>
using namespace std;
int N;
int Q;
int S;
long long Sum;
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &S);
    if (S < Q) {
      Sum += Q - S;
    } else {
      Q = S;
    }
  }
  printf("%d", Sum);
  return 0;
}
