#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, ans = 0;
  vector<int> A;
  vector<int> D;
  cin >> N;
  A.resize(N);
  D.resize(N);
  for (int i = 0; i < N; i++) cin >> A[i];
  D[0] = 0;
  for (int i = 1; i < N; i++) {
    if (A[i - 1] > A[i]) {
      D[i] = A[i - 1] - A[i];
      A[i] = A[i - 1];
    }
  }
  for (int i = 0; i < N; i++) {
    ans += D[i];
  }
  cout << ans;
  return 0;
}
