/* 
O(n ^ 3) solution
The order in which we remove elements matters. That's why we will cover all possible ways to 
remove an element.In my solution dp[i][j] represents the answer when we removed elements 
from i to j. dp[i][j] will be maximum of (dp[i][k - 1] + a[i - 1]*a[k]*a[j + 1] + dp[k + 1][j]) 
for k belonging to [i, j]. Basically we partition at different k, take the left and right 
subarray and the contribution of element at kth index.
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    
    vector<vector<int>> dp(n, vector<int>(n));
    for(int i = n - 1; i >= 0; i--) {
        for(int j = i; j < n; j++) {
            
            // calculate dp[i][j]
            for(int k = i; k <= j; k++) {
                // we already remved [i, k - 1] and [k + 1, j]
                int cur = (k - 1 >= i ? dp[i][k - 1] : 0) + (k + 1 <= j ? dp[k + 1][j] : 0);
                int my = a[k] * (i > 0 ? a[i - 1] : 1) * (j + 1 < n ? a[j + 1] : 1);
                dp[i][j] = max(dp[i][j], cur + my);
            }
        }
    }
    
    cout << dp[0][n - 1];
    
    return 0;
}