#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main(void) {
  int N, fumi = 0;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int fr, cur, dif;
  for (int i = 1; i < N; i++) {
    fr = A[i - 1];
    cur = A[i];
    dif = (fr > cur) * (fr - cur);
    A[i] += dif;
    fumi += dif;
  }
  cout << fumi;
  return 0;
}
