#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;
  long int max1 = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] >= max1) {
      max1 = arr[i];
    } else {
      total = total + max1 - arr[i];
    }
  }
  cout << total;
}
