#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long arr[n], maxa = -1;
  long long delta = 0;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 1; i < n; i++) {
    arr[i] += delta;
    if (arr[i] < arr[i - 1]) {
      delta += arr[i - 1] - arr[i];
      arr[i] = arr[i - 1];
    }
  }
  cout << delta << endl;
  return 0;
}
