#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, n;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i < n; ++i) cin >> a[i];
  int dis, cnt = 0;
  for (i = 1; i < n; ++i)
    if (a[i] < a[i - 1]) {
      dis = a[i - 1] - a[i];
      cnt += dis;
      a[i] += dis;
    }
  cout << cnt << endl;
  return 0;
}
