#include <stdio.h>
#include <stdlib.h>
int main()
{ int n,s[600][600],m[1024],i,j,k,e,maxx;
scanf("%d",&n);
e=0;
for(i=1;i<=n;i++){
scanf("%d",&m[i]);
m[i+n]=m[i];
}
for(j=2;j<=2*n;j++){
for(i=j-1;i>=1&&j-i<n;i--){
maxx=0;
for(k=i;k<=j-1;k++){
if(maxx<s[i][k]+s[k+1][j]+m[i]*m[k+1]*m[j+1]){
maxx=s[i][k]+s[k+1][j]+m[i]*m[k+1]*m[j+1];
}
}
s[i][j]=maxx;
if(s[i][j]>e){e=s[i][j];}
}
}
printf("%d",e);
return 0;
}
#include <stdlib.h>
int main()
{ int n,s[600][600],m[1024],i,j,k,e,maxx;
scanf("%d",&n);
e=0;
for(i=1;i<=n;i++){
scanf("%d",&m[i]);
m[i+n]=m[i];
}
for(j=2;j<=2*n;j++){
for(i=j-1;i>=1&&j-i<n;i--){
maxx=0;
for(k=i;k<=j-1;k++){
if(maxx<s[i][k]+s[k+1][j]+m[i]*m[k+1]*m[j+1]){
maxx=s[i][k]+s[k+1][j]+m[i]*m[k+1]*m[j+1];
}
}
s[i][j]=maxx;
if(s[i][j]>e){e=s[i][j];}
}
}
printf("%d",e);
return 0;
}