#include <bits/stdc++.h>
int main() {
  long long n;
  std::vector<long long> a;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    long long temp = 0;
    std::cin >> temp;
    a.push_back(temp);
  }
  long long temp_max = a[0];
  long long ans;
  for (int i = 1; i < n; ++i) {
    temp_max = std::max(temp_max, a[i]);
    ans += (temp_max - a[i]);
  }
  std::cout << ans << std::endl;
  return 0;
}
