#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n];
  int k = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int r;
  for (int j = 0; j < (n - 1); j++) {
    r = arr[j + 1] - arr[j];
    if (r < 0) {
      k = k - r;
      arr[j + 1] = arr[j + 1] - r;
    }
  }
  cout << k << endl;
  return 0;
}
