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