#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 ans = 0;
  if (n == 1)
    cout << "0" << endl;
  else {
    int temp = arr[0];
    for (int i = 1; i < n; i++) {
      if (temp > arr[i]) {
        ans += temp - arr[i];
      } else {
        temp = max(temp, arr[i]);
      }
    }
    cout << ans << endl;
  }
}
int32_t main() {
  solution();
  return 0;
}
