#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  int maxTall = 0, total = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (maxTall > A[i]) {
      int distance = maxTall - A[i];
      total += distance;
    } else if (maxTall < A[i]) {
      maxTall = A[i];
    }
  }
  cout << total << endl;
  return 0;
}
