#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  int arr[10000];
  int d = 0;
  int sum = 0;
  for (int i = 0; i < N; i++) cin >> arr[i];
  for (int i = 1; i < N; i++) {
    if (arr[i - 1] > arr[i]) {
      d = arr[i - 1] - arr[i];
      arr[i] += d;
      sum += d;
    }
  }
  cout << sum;
  return 0;
}
