#include <bits/stdc++.h>
using namespace std;
bool solved = 0;
void solve() {
  solved = 1;
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  };
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (A[i] < A[i - 1]) {
      ans += A[i - 1] - A[i];
      A[i] = A[i - 1];
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  if (!solved) cout << "solve() function blm dipanggil!" << endl;
  return 0;
}
