#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int arr[n];
  long long ans = 0;
  for (int i = 0; i < n; ++i) cin >> arr[i];
  for (int i = 0; i < n - 1; ++i)
    if (arr[i] > arr[i + 1]) ans += arr[i] - arr[i + 1];
  cout << ans << endl;
  return 0;
}
