#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  int A, B = 0, X = 0;
  cin >> N;
  for (long long i = 0; i < N; ++i) {
    cin >> A;
    if (A < B) {
      X += B - A;
    } else {
      B = A;
    }
  }
  cout << X;
}
