#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = (int)(0); i < (int)(N); ++i) cin >> A[i];
  int s = 0, pre = A[0];
  for (int i = (int)(1); i < (int)(N); ++i) {
    if (pre > A[i])
      s += pre - A[i];
    else
      pre = A[i];
  }
  cout << s << endl;
}
