#include <bits/stdc++.h>
int Count(int &counter, std::vector<int> list, int index) {
  std::cout << "index : " << index << std::endl;
  if (index == 0) {
    return Count(counter, list, (index + 1));
  }
  const int current = list[index];
  const int prev = list[index - 1];
  const int diff = prev - current;
  if (diff <= 0) {
  } else {
    list[index] = current + diff;
    counter += diff;
  }
  if (list.size() > static_cast<std::vector<int>::size_type>(index + 1)) {
    return Count(counter, list, (index + 1));
  } else {
    return counter;
  }
}
int main() {
  int len(0), store(0), dai(0);
  std::vector<int> list;
  std::cin >> len;
  for (int i = 0; i < len; ++i) {
    std::cin >> store;
    list.push_back(store);
  }
  std::cout << Count(dai, list, 0) << std::endl;
  return 0;
}
