#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
long long int a[200005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int n, i, sum = 0, d;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 2; i <= n; i++) {
    d = max(0ll, a[i] - a[i - 1]);
    a[i] += d;
    sum += d;
  }
  cout << sum << "\n";
}
