#include <bits/stdc++.h>
using namespace std;
template <typename T>
void DEBUG(T e) {
  if (true == false) return;
  std::cout << e << " ";
}
template <typename T>
void DEBUG(const std::vector<T>& v) {
  if (true == false) return;
  for (const auto& e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T>
void DEBUG(const std::vector<std::vector<T> >& vv) {
  if (true == false) return;
  for (const auto& v : vv) {
    DEBUG(v);
  }
}
template <class T, class... Ts>
void DEBUG(T d, Ts... e) {
  if (true == false) return;
  DEBUG(d);
  DEBUG(e...);
}
template <class T>
void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    exit(0);
  }
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  return a > b && (a = b, true);
}
struct mint {
  long long x;
  mint(long long x = 0) : x((x % 1000000007 + 1000000007) % 1000000007) {}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= 1000000007) x -= 1000000007;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += 1000000007 - a.x) >= 1000000007) x -= 1000000007;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= 1000000007;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(1000000007 - 2); }
  mint& operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};
void solve(void) {
  long long N;
  cin >> N;
  priority_queue<pair<long long, long long> > P1, P2;
  for (long long i = 0; i < N; i++) {
    long long X;
    cin >> X;
    long long L1 = abs(0 - i);
    long long L2 = abs(N - 1 - i);
    P1.push(make_pair(L1 * X, i));
    P2.push(make_pair(L2 * X, i));
  }
  long long L = 0, R = N - 1;
  long long ANS = 0;
  set<long long> SET;
  vector<long long> vec(N, 0);
  for (long long i = 0; i < N; i++) {
    pair<long long, long long> P;
    if (i % 2 == 0) {
      while (true) {
        P = P1.top();
        P1.pop();
        if (SET.find(P.second) == SET.end()) break;
      }
      long long L1 = P.first / P.second;
      ANS += L1 * abs(L - P.second);
      SET.insert(P.second);
      vec[L] = P.second;
      L++;
    } else {
      while (true) {
        P = P2.top();
        P2.pop();
        if (SET.find(P.second) == SET.end()) break;
      }
      long long L1 = P.first / (N - 1 - P.second);
      ANS += L1 * abs(R - P.second);
      SET.insert(P.second);
      vec[R] = P.second;
      R--;
    }
  }
  cout << ANS << endl;
  return;
}
int32_t main(int32_t argc, const char* argv[]) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout << std::fixed;
  std::cout << std::setprecision(9);
  solve();
  return 0;
}
