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