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