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