#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1 << 18;
int case_no = 1;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, mxm, ans = 0;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  mxm = arr[0];
  for (int i = 1; i < n; i++) {
    if (mxm > arr[i]) {
      ans += abs(arr[i] - mxm);
    }
    mxm = max(mxm, arr[i]);
  }
  cout << ans << endl;
  return 0;
}
