#include <bits/stdc++.h>
int main() {
  long long n;
  std::cin >> n;
  long long a[n];
  long long max = 0;
  for (long long i = 0; i < n; i++) {
    std::cin >> a[i];
    max = std::max(a[i], max);
  }
  long long ind = -1;
  for (long long i = 0; i < n; i++) {
    if (a[i] == max) {
      ind = i;
      break;
    }
  }
  long long t = 0;
  for (long long i = ind; i < n; i++) {
    if (a[i] < max) t += max - a[i];
  }
  for (long long i = 0; i < ind; i++) {
    if (a[i] > a[i + 1]) t += a[i] - a[i + 1];
  }
  std::cout << t;
  return 0;
}
