#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(void) {
  int n;
  int a[200000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max = a[0];
  int sum = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] >= max) {
      max = a[i];
    } else {
      sum += max - a[i];
    }
  }
  cout << sum;
  return 0;
}
