#include <bits/stdc++.h>
int main() {
  int count;
  std::cin >> count;
  int max = 0;
  int sum = 0;
  for (int i = 0; i < count; i++) {
    int tall;
    std::cin >> tall;
    sum += std::max(max - tall, 0);
    max = std::max(tall, max);
  }
  std::cout << sum << std::endl;
  return 0;
}
