#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int sum = 0;
  vector<int> a(n);
  for (int i = 0; i < (int)(n); ++i) cin >> a[i];
  int t = a[0];
  for (int i = 1; i < n; ++i) {
    t = max(t, a[i]);
    if (t > a[i]) {
      sum += t - a[i];
    }
  }
  cout << sum << endl;
  return 0;
}
