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