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