#include <bits/stdc++.h>
int main() {
  int n;
  int *a, *dai, *wa;
  std::cin >> n;
  a = new int[n];
  dai = new int[n];
  wa = new int[n];
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
  wa[0] = a[0];
  dai[0] = 0;
  for (int j = 0; j < n - 1; j++) {
    if (a[j] <= a[j + 1]) {
      dai[j + 1] = 0;
      wa[j + 1] = a[j + 1];
    } else {
      dai[j + 1] = wa[j] - a[j + 1];
      wa[j + 1] = dai[j + 1] + a[j + 1];
    }
  }
  int sum = 0;
  for (int num = 0; num < n; num++) {
    sum += dai[num];
  }
  std::cout << sum << std::endl;
  delete[] a;
  delete[] dai;
  delete[] wa;
  return 0;
}
