1 #include<cstdio>
2 #include<iostream>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 using namespace std;
7 typedef long long int64;
8 char ch;
9 bool ok;
10 void read(int &x){
11 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch==‘-‘) ok=1;
12 for (x=0;isdigit(ch);x=x*10+ch-‘0‘,ch=getchar());
13 if (ok) x=-x;
14 }
15 void read(int64 &x){
16 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch==‘-‘) ok=1;
17 for (x=0;isdigit(ch);x=x*10+ch-‘0‘,ch=getchar());
18 if (ok) x=-x;
19 }
20 const int maxn=3005;
21 int n;
22 int64 a[maxn],sum[maxn],g[maxn][maxn],f[maxn];
23 inline int64 s(int l,int r){return sum[r]-sum[l-1];}
24 int main(){
25 read(n);
26 for (int i=1;i<=n;i++) read(a[i]);
27 for (int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
28 for (int i=1;i<=n;i++){
29 g[i][i]=0;
30 for (int j=i-1;j>=1;j--) g[i][j]=g[i][j+1]+s(j+1,i)+min(3LL*(i-j)*a[j],s(j+1,i));
31 }
32 memset(f,63,sizeof(f));
33 f[0]=0;
34 for (int i=1;i<=n;i++) for (int j=0;j<i;j++) f[i]=min(f[i],f[j]+g[i][j+1]+s(i+1,n)*(4LL*(i-j)-2));
35 printf("%lld\n",f[n]);
36 return 0;
37 }