#include <iostream>
#include <cstdio>
using namespace std;
int a[210],f[210][210];
int maxint(int a,int b)
{
if(a>b)
return(a);
return(b);
}
int main(void)
{
freopen("energy.in","r",stdin);
freopen("energy.out","w",stdout);
int i,j,k,n,n2,l,r,temp=0;
cin>>n;
for (i=1,n2=n+1;i<=n;i++,n2++)
{
cin>>a[i];
a[n2]=a[i];
}
n--;
n2-=3;
for (i=1;i<=n2;i++)
f[i][1]=a[i]*a[i+1]*a[i+2];
for (i=1;i<n2;i++)
{
f[i][2]=f[i+1][1]+a[i]*a[i+1]*a[i+3];//f[][] i+1 to i+1
f[i][2]=maxint(f[i][2],f[i][1]+a[i]*a[i+2]*a[i+3]);//f[][] i to i
}
for (j=3;j<=n;j++)
for (i=1;i+j-1<=n2;i++)
{
f[i][j]=f[i+1][j-1]+a[i]*a[i+1]*a[i+j+1];//f[][] i+1 to i+j-1
f[i][j]=maxint(f[i][j],f[i][j-1]+a[i]*a[i+j]*a[i+j+1]);//f[][] i to i+j-2
l=i+1;
r=i+j-2;
for (k=l;k<=r;k++)
f[i][j]=maxint(f[i][j],f[i][k-i]+f[k+1][i+j-k-1]+a[i]*a[k+1]*a[i+j+1]);//f[][] i+1 to k-1 && k+1 to r
}
for (i=1;i<=n+1;i++)
if (temp<f[i][n])
temp=f[i][n];
cout<<temp<<endl;
return(0);
}