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