#include <iostream>
#include <string>

int main() {

  int N;
  std::cin >> N;

  int A[200'000];

  int sum = 0;
  int heightPrev = 0;

  for (int i=0; i<N; ++i) {
    int t = 0;
    std::cin >> t;

    if (heightPrev > t) {
      sum += heightPrev - t;
    }

    heightPrev = std::max(heightPrev, t);
  }

  std::cout << sum << std::endl;

  return 0;

}
