#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
struct items{
int v,num,l;
};
int cmp(items a,items b){
return a.v<b.v;
}
int ccmp(items a,items b){
return a.l<b.l;
}
items a[2001];
int n;
ll ans;
int main(){
freopen("treats.in","r",stdin);
freopen("treats.out","w",stdout);
cin>>n;
for(int i = 1;i<=n;i++){
cin>>a[i].v;
a[i].l = i;
}
sort(a+1,a+n+1,cmp);
for(int i = 1;i<=n;i++){
a[i].num = i;
}
sort(a+1,a+n+1,ccmp);
int l = 1,r = n;
for(int i = 1;i<=n;i++){
if(a[l].num<a[r].num){
ans = ans + a[l].v * i;
l = l + 1;
}
else{
ans = ans + a[r].v * i;
r = r - 1;
}
if(l==r + 1){
cout<<ans;
break;
}
}
return 0;
}