#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j, k, m, n, ct = 0;
  string s;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i <= n - 1; i++) cin >> a[i];
  ;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    ans += max(a[i - 1] - a[i], 0);
    a[i] = max(a[i], a[i - 1]);
  }
  cout << ans << "\n";
  return 0;
}
