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