#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  int stool = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i > 0) {
      int curr = max(a[i - 1] - a[i], 0);
      a[i] += curr;
      stool += curr;
    }
  }
  cout << stool << '\n';
  return 0;
}
