#include <bits/stdc++.h>

using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
using f80 = __float80;

template<typename C>
i64 Size(const C& c) { return static_cast<i64>(c.size()); }

template<typename C>
void Sort(C &c) { std::sort(c.begin(), c.end()); }

template<typename C>
void SortR(C &c) { std::sort(c.rbegin(), c.rend()); }

template<typename T, typename U, typename Comp=std::less<>>
bool ChMax(T& x_max, const U& x, Comp comp={}) {
  if(comp(x_max, x)) {
    x_max = x;
    return true;
  }
  return false;
}

template<typename T, typename U, typename Comp=std::less<>>
bool ChMin(T& x_min, const U& x, Comp comp={}) {
  if(comp(x, x_min)) {
    x_min = x;
    return true;
  }
  return false;
}

struct ProconInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;

  ProconInit() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(IOS_PREC);
    if (AUTOFLUSH) std::cout << std::unitbuf;
  }
} PROCON_INIT;

void solve() {
  i64 n; std::cin >> n;
  std::vector<i64> a(n); for (auto &i : a) std::cin >> i;

  std::deque<i64> d(n);
  for (i64 i = 0; i < n; i++) d.at(i) = i;

  std::vector<std::pair<i64, i64>> p(n);
  for (i64 i = 0; i < n; i++) p.at(i) =  std::make_pair(a.at(i), i);
  SortR(p);

  i64 sum = 0;
  for (auto &i : p) {
    i64 f = std::abs(d.front() - i.second);
    i64 b = std::abs(d.back() - i.second);

    if (f > b) {
      sum += i.first * f;
      d.pop_front();
    } else {
      sum += i.first * b;
      d.pop_back();
    }
  }

  std::cout << sum << "\n";
}

signed main() {
  solve();

  return 0;
}
