#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
inline long long add(long long a, long long b) {
  long long res = a + b;
  if (res >= mod) res -= mod;
  return res;
}
inline long long sub(long long a, long long b) {
  long long res = a - b;
  if (res < 0) res += mod;
  return res;
}
inline long long mul(long long a, long long b) { return (a * b) % mod; }
long long modpow(long long a, long long b) {
  if (a == 0 || a == 1 || b == 1) return a;
  long long ans = 1;
  while (b) {
    if (b % 2) ans = mul(ans, a);
    b /= 2;
    a = mul(a, a);
  }
  return ans;
}
long long solve(long long K, vector<long long> &a, long long n) {
  vector<long long> b, c;
  for (long long i = 0; i < n; i++) {
    if (i < K)
      b.push_back(a[i]);
    else
      c.push_back(a[i]);
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    if (i < K)
      ans -= a[i] * (i + 1);
    else
      ans += a[i] * (i + 1);
  }
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  reverse(b.begin(), b.end());
  long long cur = n;
  for (long long i = 0; i < (long long)b.size(); i++) {
    ans += b[i] * cur;
    cur--;
  }
  reverse(c.begin(), c.end());
  cur = 1;
  for (long long i = 0; i < (long long)c.size(); i++) {
    ans -= c[i] * cur;
    cur++;
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long ans = 0;
  for (long long K = 0; K < n; K++) {
    ans = max(ans, solve(K, a, n));
  }
  cout << ans << "\n";
  return 0;
}
