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