#include <bits/stdc++.h>
using namespace std;
vector<string> split(const string& s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c)) v.emplace_back(x);
  return move(v);
}
template <typename T, typename... Args>
inline string arrStr(T arr, int n) {
  stringstream s;
  s << "[";
  for (int i = 0; i < n - 1; i++) s << arr[i] << ",";
  s << arr[n - 1] << "]";
  return s.str();
}
inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template <typename T>
inline void __evars_out_var(vector<T> val) {
  cerr << arrStr(val, val.size());
}
template <typename T>
inline void __evars_out_var(T* val) {
  cerr << arrStr(val, 10);
}
template <typename T>
inline void __evars_out_var(T val) {
  cerr << val;
}
inline void __evars(vector<string>::iterator it) { cerr << "\n"; }
template <typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
  __evars_out_var(a);
  cerr << "; ";
  __evars(++it, args...);
}
long long ans = 0;
int ind;
void brute(vector<int> a, vector<int> b) {
  long long ans = 0;
  for (int i = 0; i < (int)a.size(); ++i) {
    auto pos = find(b.begin(), b.end(), a[i]);
    int index = distance(b.begin(), pos);
    int score = abs(index - i);
    ans += score * a[i];
  }
  cout << ans << "\n";
}
bool lft;
pair<int, int> maxsc(vector<int> a, vector<int> npos, vector<int> opos) {
  pair<int, int> top;
  for (int i = 0; i < (int)a.size(); ++i) {
    int lmax, rmax;
    rmax = a[i] * abs(opos[i] - npos.front());
    lmax = a[i] * abs(opos[i] - npos.back());
    if (a[i] == 3 && (int)a.size() == 3) {
      {
        __evars_begin(65);
        __evars(split("opos[i], npos[i], lmax, rmax", ',').begin(), opos[i],
                npos[i], lmax, rmax);
      };
    }
    int omax = max(lmax, rmax);
    if (omax > top.first) {
      if (rmax > lmax)
        lft = true;
      else
        lft = false;
      top.first = omax;
      top.second = i;
    }
  }
  return top;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  bool nodd = false;
  if (n & 1) nodd = true;
  vector<int> a(n);
  vector<int> pos(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    pos[i] = i;
  }
  vector<int> npos(pos.begin(), pos.end());
  pair<int, int> top;
  for (int i = 0; a.empty() == false; ++i) {
    top = maxsc(a, npos, pos);
    ans += top.first;
    {
      __evars_begin(102);
      __evars(
          split("a, pos, npos, ans, top.first, top.second, lft", ',').begin(),
          a, pos, npos, ans, top.first, top.second, lft);
    };
    if (lft) {
      for (int j = 0; j < (int)npos.size(); ++j) {
        if (j < top.second)
          npos[j]++;
        else
          break;
      }
    } else {
      for (int j = (int)npos.size() - 1; j >= 0; --j) {
        if (j > top.second)
          npos[j]--;
        else
          break;
      }
    }
    a.erase(a.begin() + top.second);
    pos.erase(pos.begin() + top.second);
    npos.erase(npos.begin() + top.second);
  }
  cout << ans << "\n";
  return 0;
  int lcount = 0;
  int rcount = 0;
  for (int j = 0; !a.empty(); ++j) {
    int s = (int)a.size(), ind = 0;
    pair<int, int> top;
    for (int i = 0; i < s; ++i) {
      int temp = a[i], score;
      if (nodd)
        score = pos[i] + 1 <= (s + 1) / 2 ? s - 1 - pos[i] : pos[i];
      else {
        if (pos[i] + 1 == s / 2 || pos[i] + 1 == s / 2 + 1)
          score = s / 2;
        else
          score = pos[i] < s / 2 ? s - 1 - pos[i] : pos[i];
      }
      if (score * temp > top.first) {
        ind = i;
        top.first = score * temp;
        top.second = temp;
      }
      if (score * temp == top.first && temp < top.second) {
        ind = i;
        top.second = temp;
      }
    }
    {
      __evars_begin(151);
      __evars(split("top.first, ind, a, pos", ',').begin(), top.first, ind, a,
              pos);
    };
    ans += top.first;
    a.erase(a.begin() + ind);
    pos.erase(pos.begin() + ind);
  }
  cout << ans;
}
