#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, A;
  int total = 0;
  cin >> N;
  vector<int> vec(N);
  vector<int> high{0};
  for (int i = 0; i < N; i++) {
    cin >> A;
    vec.at(i) = A;
  }
  for (int j = 1; j < N; j++) {
    if (vec.at(j) < vec.at(j - 1)) {
      high.push_back(vec.at(j - 1) - vec.at(j));
      vec.at(j) = vec.at(j - 1);
    }
  }
  for (int k = 0; k < high.size(); k++) total += high.at(k);
  cout << total << endl;
}
