#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &V) {
  os << '[';
  for (int i = 0; i < (int)V.size(); i++)
    os << V[i] << ",]"[i == (int)V.size() - 1];
  return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &V) {
  for (T &x : V) is >> x;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '{' << p.first << ',' << p.second << '}';
  return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T>
void minself(T &a, T &b) {
  a = min(a, b);
}
template <typename T>
void maxself(T &a, T &b) {
  a = max(a, b);
}
int main() {
  cin.tie(0), cin.sync_with_stdio(0), cout.tie(0), cout.sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> v(n), ans(n);
  cin >> v;
  vector<pair<int, int>> ord(n);
  for (int i = 0; i < n; i++) ord[i] = {v[i], i};
  sort(ord.rbegin(), ord.rend());
  int start = 0, end = n - 1, score = 0;
  for (int i = 0; i < n; i++) {
    int val = ord[i].first, ind = ord[i].second;
    int sin = val * (ind - start);
    int des = val * (end - ind);
    for (int j = 0; j < ind; j++)
      if (v[j] < val) sin += v[j];
    for (int j = ind + 1; j < n; j++)
      if (v[j] < val) des += v[j];
    if (sin >= des) {
      ans[start] = val;
      score += (ind - start) * val;
      start++;
    } else {
      ans[end] = val;
      score += (end - ind) * val;
      end--;
    }
  }
  cout << score;
}
