#include <bits/stdc++.h>
using namespace std;
void solution() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  int a = arr[0];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a > arr[i]) {
      ans += a - arr[i];
    } else {
      a = max(a, arr[i]);
    }
  }
  if (n != 1)
    cout << ans << endl;
  else
    cout << "0" << endl;
}
int32_t main() {
  solution();
  return 0;
}
