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