#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max = a[0];
  for (auto &i : a) {
    if (i >= max) {
      max = i;
    } else {
      t += (max - i);
    }
  }
  cout << t;
}
