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