#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }
  int min = arr[0];
  int val = 0;
  int max = arr[0];
  for (int i = 0; i < n; i++) {
    if (arr[i] < min) {
      min = arr[i];
      val += (max - min);
    } else if (arr[i] > max) {
      min = arr[i];
      max = arr[i];
    } else if (arr[i] >= min && arr[i] <= max) {
      val += (max - arr[i]);
    }
  }
  cout << val;
  return 0;
}
