#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
void solve() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int c = 0; c < n; c++) cin >> arr[c];
  int sum = 0;
  for (int c = 1; c < n; c++)
    sum += max(0, arr[c - 1] - arr[c]), arr[c] = max(arr[c], arr[c - 1]);
  cout << sum << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  for (int tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}
