#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> A;
  cin >> N;
  A.resize(N);
  for (int i = 0; i < N; i++) cin >> A[i];
  int s = 0;
  int max = 0;
  int dif;
  for (int i = 0; i < N; i++) {
    dif = max - A[i];
    s += dif > 0 ? dif : 0;
    max = dif > 0 ? max : A[i];
  }
  cout << s << endl;
  return (0);
}
