#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()

#define READ(x) (cin >> (x))

#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))

struct Baby {
  size_t orig_pos;
  ll activity;
};

ll calc_happiness(const vector<Baby> &Baby_vec, auto itr, ll left_bound, ll right_bound) {
  const Baby &baby = *itr;
  ll left_d = baby.orig_pos - left_bound;
  ll right_d = right_bound - baby.orig_pos;
  
  if (itr == Baby_vec.end()) {
    return 0;
  }
  if (left_d > right_d) {
    return left_d * baby.activity +
        calc_happiness(Baby_vec, ++itr, ++left_bound, right_bound);
  } else if (left_d < right_d) {
    return right_d * baby.activity +
        calc_happiness(Baby_vec, ++itr, left_bound, --right_bound);
  } else {
    return max(
        left_d * baby.activity +
        calc_happiness(Baby_vec, ++itr, ++left_bound, right_bound),
        right_d * baby.activity +
        calc_happiness(Baby_vec, ++itr, left_bound, --right_bound)
               );
  }
}

int main() {
  // get values from input
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  READ(N);

  vector<ll>  A(N);
  vector<Baby> Baby_vec(N);
  for (size_t i = 0; i < N; ++i) {
    READ(A[i]);
    Baby_vec[i] = Baby{i, A[i]};
  }

  // main procedure
  sort(ALL(Baby_vec), [](Baby &x, Baby &y) -> bool {return x.activity > y.activity;});
  
  ll happiness_sum = calc_happiness(Baby_vec, Baby_vec.begin(), 0, N-1);
  // output
  WRITE_N(happiness_sum);

  return 0;
}
  
