#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> a;
  for (int i = 0; i < n; i++) {
    long long num;
    cin >> num;
    a.push_back(num);
  }
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i]) {
      ans += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  cout << ans << "\n";
  ;
}
