#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int *A;
  A = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int now_height = 0;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] >= now_height) {
      now_height = A[i];
    } else {
      sum += (now_height - A[i]);
    }
  }
  cout << sum;
  delete[] A;
  return 0;
}
