#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class CasketOfStar { public:
int maxEnergy(vector <int> weight)
{
return rec(weight, 0, weight.size()-1);
}
map<pair<int,int>, int> memo;
int rec(const vector<int>& w, int L, int R)
{
if( L+1 == R )
return 0;
pair<int,int> key(L,R);
if( memo.count(key) )
return memo[key];
int best = 0;
for(int C=L+1; C<R; ++C)
best = max(best, rec(w, L, C) + rec(w, C, R));
return memo[key] = best + w[L]*w[R];
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, CasketOfStar().maxEnergy(weight));}
int main(){
CASE(0)
int weight_[] = {1,2,3,4};
vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_));
int _ = 12;
END
CASE(1)
int weight_[] = {100,2,1,3,100};
vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_));
int _ = 10400;
END
CASE(2)
int weight_[] = {2,2,7,6,90,5,9};
vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_));
int _ = 1818;
END
CASE(3)
int weight_[] = {477,744,474,777,447,747,777,474};
vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_));
int _ = 2937051;
END
CASE(4)
int weight_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_));
int _ = 13;
END
/*
CASE(5)
int weight_[] = ;
vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_));
int _ = ;
END
CASE(6)
int weight_[] = ;
vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_));
int _ = ;
END
*/
}
// END CUT HERE