#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  int mx = 0, res = 0;
  for (int i = 0; i < (n); ++i) {
    if (a[i] > mx)
      mx = a[i];
    else {
      res += mx - a[i];
    }
  }
  cout << res << '\n';
}
