#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, sum = 0;
  cin >> n;
  int *A = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n - 1; i++) {
    if (A[i] - A[i + 1] > 0) {
      sum += A[i] - A[i + 1];
      A[i + 1] += A[i] - A[i + 1];
    }
  }
  cout << sum;
}
