#include <bits/stdc++.h>
using namespace std;
int n, a, t = 0, m = 0;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (t < a)
      t = a;
    else {
      m += t - a;
    }
  }
  cout << m << endl;
}
