#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  long long A[200000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int NowSin = 0;
  int MinFumi = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] > NowSin)
      NowSin = A[i];
    else
      MinFumi += (NowSin - A[i]);
  }
  cout << MinFumi << endl;
}
