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