#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  int *A;
  A = new int[N];
  for (int i = 0; i < N; i++) {
    A[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int *B;
  B = new int[N];
  for (int i = 0; i < N; i++) {
    B[i] = 0;
  }
  for (int i = 0; i < N - 1; i++) {
    if (A[i] > A[i + 1]) {
      int C = A[i] - A[i + 1];
      A[i + 1] += C;
      B[i + 1] += C;
    }
  }
  int all = 0;
  for (int i = 0; i < N; i++) {
    all += B[i];
  }
  cout << all << endl;
  return 0;
}
