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