#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < (n); ++i) {
    cin >> a[i];
  }
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i - 1] > a[i]) {
      sum += a[i - 1] - a[i];
      a[i] += a[i - 1] - a[i];
    }
  }
  cout << sum << endl;
}
