#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using P = pair<int, int>;
const int INF = 1e9;
const int E = numeric_limits<int>::min();
int main(void) {
  int N;
  cin >> N;
  vector<LL> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int stepLadder = 0;
  for (int i = 1; i < N; i++) {
    if (A[i] < A[i - 1]) {
      stepLadder += A[i - 1] - A[i];
      A[i] += A[i - 1] - A[i];
    }
  }
  printf("%d\n", stepLadder);
  return 0;
}
