#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int sum = 0;
  int m = arr[0];
  for (int i = 0; i < n; i++) {
    if (m > arr[i]) {
      sum += (m - arr[i]);
    } else {
      m = arr[i];
    }
  }
  cout << sum << "\n";
  return 0;
}
