#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  if (n % 2 == 0) {
    vector<pair<long long, int>> a(n / 2), b(n / 2);
    for (int i = 0; i < n / 2; i++) {
      cin >> a[i].first;
      a[i].second = i;
    }
    for (int i = 0; i < n / 2; i++) {
      cin >> b[i].first;
      b[i].second = n / 2 + i;
    }
    sort(a.begin(), a.end());
    sort(b.rbegin(), b.rend());
    long long ans = 0;
    for (int i = 0; i < n / 2; i++) {
      ans += b[i].first * abs(i - b[i].second);
    }
    for (int i = n / 2; i < n; i++) {
      ans += a[i - n / 2].first * abs(i - a[i - n / 2].second);
    }
    cout << ans << endl;
  } else {
    vector<pair<long long, int>> a(n / 2), b(n / 2 + 1), c(n / 2 + 1), d(n / 2);
    for (int i = 0; i < n / 2; i++) {
      cin >> a[i].first;
      a[i].second = i;
      c[i] = a[i];
    }
    cin >> b[0].first;
    b[0].second = n / 2;
    c[n / 2] = b[0];
    for (int i = 0; i < n / 2; i++) {
      cin >> b[i + 1].first;
      b[i + 1].second = n / 2 + i + 1;
      d[i] = b[i + 1];
    }
    sort(a.begin(), a.end());
    sort(b.rbegin(), b.rend());
    sort(c.begin(), c.end());
    sort(d.rbegin(), d.rend());
    long long ans1 = 0, ans2 = 0;
    for (int i = 0; i < n / 2 + 1; i++) {
      ans1 += b[i].first * abs(i - b[i].second);
    }
    for (int i = n / 2 + 1; i < n; i++) {
      ans1 += a[i].first * abs(i - a[i - n / 2 - 1].second);
    }
    for (int i = 0; i < n / 2; i++) {
      ans1 += d[i].first * abs(i - d[i].second);
    }
    for (int i = n / 2; i < n; i++) {
      ans1 += c[i].first * abs(i - c[i - n / 2].second);
    }
    cout << max(ans1, ans2) << endl;
  }
  return 0;
}
