#include <bits/stdc++.h>
using namespace std;
using in = int64_t;
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (int)(n); i++) cin >> a.at(i);
  vector<P> x(n);
  for (int i = 0; i < (int)(n); i++) x[i] = P(a[i], -i);
  sort(x.begin(), x.end());
  deque<int> make;
  make.emplace_back(x[0].first);
  int pla = -x[0].second;
  for (int i = (int)(1); i < (int)(n); i++) {
    int pla2 = -x[i].second;
    if (pla < pla2)
      make.emplace_front(x[i].first);
    else
      make.emplace_back(x[i].first);
    pla = pla2;
  }
  int ans = 0;
  vector<bool> seen(n, 1);
  for (int i = 0; i < (int)(n); i++) {
    int maked = make[i];
    for (int j = 0; j < (int)(n); j++) {
      if (maked == a[j] && seen[j]) {
        ans += maked * abs(j - i);
        seen[j] = 0;
        break;
      }
    }
  }
  cout << ans << endl;
}
