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