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