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