#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> vec;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    vec.push_back(a);
  }
  int count = 0;
  int mx = vec[0];
  for (int i = 0; i < vec.size() - 1; i++) {
    mx = max(vec[i], mx);
    if (vec[i] > vec[i + 1]) {
      count += mx - vec[i + 1];
      mx = max(vec[i], mx);
    }
  }
  cout << count << endl;
}
