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