#include <bits/stdc++.h>
using namespace std;
bool comp(pair<int, int> &a, pair<int, int> &b) { return a.first < b.first; }
void solve() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  int lt = arr[0], sum{0};
  for (int i = 1; i < n; ++i) {
    if (arr[i] < lt) {
      sum += (lt - arr[i]);
      arr[i] = arr[i] + (lt - arr[i]);
    }
    lt = arr[i];
  }
  cout << sum;
}
int32_t main() {
  std::ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int testcases = 1;
  while (testcases--) {
    solve();
  }
  return 0;
}
