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