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