#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int h = 0;
  int k;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    cin >> k;
    v.push_back(k);
  }
  for (int j = 0; j < n - 1; j++) {
    if (v.at(j + 1) < v.at(j)) {
      h = h + (v.at(j) - v.at(j + 1));
      v.at(j + 1) = v.at(j);
    }
  }
  cout << h;
}
