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