#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<long long int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int size = 0;
  int curr = arr[0];
  for (int i = 1; i < n; i++) {
    if (arr[i] < curr) {
      size += curr - arr[i];
      arr[i] += curr - arr[i];
    }
    curr = arr[i];
  }
  cout << size << endl;
}
int main() { solve(); }
