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