#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
const int MAXN = 101;
int n, mar[MAXN * 2], m[MAXN * 2][MAXN * 2] = {0}, ans = 0;
ifstream fi("energy.in");
ofstream fo("energy.out");
#define cin fi
#define cout fo
main()
{
ios::sync_with_stdio(false);
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> mar[i];
mar[i + n] = mar[i];
}
for(int len = 2; len <= n; len ++)
for(int i = 1; i <= 2 * n - len + 1; i ++)
{
int j = i + len - 1, maxx = 0;
for(int k = i; k < j; k ++)
{
maxx=m[i][k]+m[k+1][j]+mar[i]*mar[k+1]*mar[j+1];
m[i][j] = max(maxx, m[i][j]);
}
}
for(int ia = 1; ia <= n; ia ++)
ans = max(ans, m[ia][ia + n - 1]);
cout << ans/* << endl*/;
}