#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  vector<int> v;
  cin >> n;
  while (n--) {
    cin >> x;
    v.push_back(x);
  }
  int minimus = 0;
  long long maximus = 0;
  long long res = 0;
  int cnt = 0;
  for (int i = 0; i < v.size() - 1; i++) {
    if (v[i] > v[i + 1]) {
      res += v[i] - v[i + 1];
    }
  }
  cout << res;
  return 0;
}
