#include <bits/stdc++.h>
int main() {
  int N = 0, answer = 0;
  long long Apre = 0, Apost = 0;
  std::cin >> N;
  for (int number = 0; number < N; ++number) {
    if (number == 0) {
      std::cin >> Apre;
      continue;
    }
    std::cin >> Apost;
    if (Apost < Apre) {
      answer += Apre - Apost;
    } else {
      Apre = Apost;
    }
  }
  std::cout << answer << std::endl;
}
