#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int cnt = 0;
  long long *A;
  cin >> N;
  A = new long long[N];
  for (int i = 0; i < N; i++) {
    if (scanf("%lld", &A[i]) == 1)
      ;
  }
  for (int i = 1; i < N; i++) {
    if (A[i - 1] > A[i]) {
      int x = A[i - 1] - A[i];
      cnt += x;
      A[i] = A[i - 1];
    }
  }
  cout << cnt << endl;
}
