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