#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < (int)(N); i++) {
    cin >> A.at(i);
  }
  int steps_sum = 0;
  int front_height = 0;
  for (int i = 0; i < (int)(N); i++) {
    if (front_height > A.at(i))
      steps_sum += (front_height - A.at(i));
    else
      front_height = A.at(i);
  }
  cout << steps_sum << endl;
  return 0;
}
