#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int maxm;
  cin >> maxm;
  int cnt = 0;
  for (int i = 1; i < n; i++) {
    int x;
    cin >> x;
    if (x >= maxm) {
      maxm = x;
      continue;
    } else {
      cnt += (maxm - x);
    }
  }
  cout << cnt << endl;
}
