#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
int main() {
  int i, c = 0;
  ll n;
  cin >> n;
  ll a[n];
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = 0; i <= n - 2; i++) {
    if (a[i + 1] < a[i]) {
      c = c + (a[i] - a[i + 1]);
      a[i + 1] = a[i + 1] + (a[i] - a[i + 1]);
    }
  }
  cout << c << endl;
}
