#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int arr[n], sum(0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
  }
  for (int i = 1; i < n; i++) {
    if (arr[i - 1] > arr[i]) {
      sum += arr[i - 1] - arr[i];
      arr[i] += (arr[i - 1] - arr[i]);
    }
  }
  printf("%d", sum);
  return 0;
}
