#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
using Graph = vector<vector<int>>;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < (int)(n); i++) cin >> a[i];
  ll tmp = 0;
  ll sum = 0;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i]) {
      tmp = a[i - 1] - a[i];
      a[i] += tmp;
      sum += tmp;
    }
  }
  int ans = sum;
  cout << ans << endl;
  return 0;
}
