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