#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k = -1, mt = 0;
  cin >> n;
  long long int ar[n + 5];
  long long int ans = 0;
  vector<long long int> v;
  ar[0] = -1;
  for (long long int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  long long int nn = 0;
  long long int cnt = 0;
  ar[n] = *max_element(ar, ar + n);
  long long int mx = ar[0];
  for (long long int i = 1; i < n; i++) {
    if (ar[i] < ar[i - 1]) {
      nn = mx - ar[i];
      cnt += nn;
    } else {
      mx = max(mx, ar[i]);
    }
  }
  cout << cnt << endl;
}
