#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, cnt = 0;
  cin >> N;
  long long A[N], max_ = 0;
  for (long long i = 0; i < N; ++i) cin >> A[i];
  for (long long i = 0; i < N - 1; ++i) {
    long long temp = A[i];
    max_ = max(max_, temp);
    if (A[i] > A[i + 1]) cnt += max_ - A[i + 1];
  }
  cout << cnt;
}
