#include <bits/stdc++.h>
using namespace std;
int main() {
  int test = 1;
  while (test--) {
    int n;
    cin >> n;
    int a[n], store[n], tot = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i], tot += a[i];
    }
    for (int i = n - 1; i >= 0; i--) {
      store[i] = a[i];
      if (i < n - 1) store[i] = max(store[i + 1], store[i]);
    }
    int mx = 0, sum = 0, ans = INT_MAX;
    for (int i = 0; i < n; i++) {
      mx = max(mx, a[i]);
      sum += a[i];
      if (i != n - 1) {
        ans = min(
            ans, (i + 1) * mx - sum + store[i + 1] * (n - i - 1) - (tot - sum));
      } else
        ans = min(ans, (i + 1) * mx - sum);
    }
    cout << ans << '\n';
  }
  return 0;
}
