#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
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, 0));
int maxSize = 0;
for (int i = 0; i < N; i++) {
dp[i][i] = A[i];
maxSize = max(maxSize, A[i]);
}
for (int len = 2; len <= N; len++) {
for (int i = 0; i + len - 1 < N; i++) {
int j = i + len - 1;
for (int k = i; k < j; k++) {
if (dp[i][k] && dp[k + 1][j] && dp[i][k] == dp[k + 1][j]) {
dp[i][j] = dp[i][k] + dp[k + 1][j];
maxSize = max(maxSize, dp[i][j]);
}
}
for (int a = i, b = j; a < b - 1;) {
int leftSum = dp[i][a];
int rightSum = dp[b][j];
if (leftSum && rightSum && leftSum == rightSum && dp[a + 1][b - 1]) {
dp[i][j] = leftSum + dp[a + 1][b - 1] + rightSum;
maxSize = max(maxSize, dp[i][j]);
break;
} else if (leftSum < rightSum) {
a++;
} else {
b--;
}
}
}
}
cout << maxSize << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCkgewogICAgaW50IE47CiAgICBjaW4gPj4gTjsKICAgIHZlY3RvcjxpbnQ+IEEoTik7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IE47IGkrKykgewogICAgICAgIGNpbiA+PiBBW2ldOwogICAgfQogICAgCiAgICB2ZWN0b3I8dmVjdG9yPGludD4+IGRwKE4sIHZlY3RvcjxpbnQ+KE4sIDApKTsKICAgIAogICAgaW50IG1heFNpemUgPSAwOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBOOyBpKyspIHsKICAgICAgICBkcFtpXVtpXSA9IEFbaV07CiAgICAgICAgbWF4U2l6ZSA9IG1heChtYXhTaXplLCBBW2ldKTsKICAgIH0KICAgIAogICAgZm9yIChpbnQgbGVuID0gMjsgbGVuIDw9IE47IGxlbisrKSB7ICAKICAgICAgICBmb3IgKGludCBpID0gMDsgaSArIGxlbiAtIDEgPCBOOyBpKyspIHsKICAgICAgICAgICAgaW50IGogPSBpICsgbGVuIC0gMTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGZvciAoaW50IGsgPSBpOyBrIDwgajsgaysrKSB7CiAgICAgICAgICAgICAgICBpZiAoZHBbaV1ba10gJiYgZHBbayArIDFdW2pdICYmIGRwW2ldW2tdID09IGRwW2sgKyAxXVtqXSkgewogICAgICAgICAgICAgICAgICAgIGRwW2ldW2pdID0gZHBbaV1ba10gKyBkcFtrICsgMV1bal07CiAgICAgICAgICAgICAgICAgICAgbWF4U2l6ZSA9IG1heChtYXhTaXplLCBkcFtpXVtqXSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIGZvciAoaW50IGEgPSBpLCBiID0gajsgYSA8IGIgLSAxOykgewogICAgICAgICAgICAgICAgaW50IGxlZnRTdW0gPSBkcFtpXVthXTsKICAgICAgICAgICAgICAgIGludCByaWdodFN1bSA9IGRwW2JdW2pdOwogICAgICAgICAgICAgICAgaWYgKGxlZnRTdW0gJiYgcmlnaHRTdW0gJiYgbGVmdFN1bSA9PSByaWdodFN1bSAmJiBkcFthICsgMV1bYiAtIDFdKSB7CiAgICAgICAgICAgICAgICAgICAgZHBbaV1bal0gPSBsZWZ0U3VtICsgZHBbYSArIDFdW2IgLSAxXSArIHJpZ2h0U3VtOwogICAgICAgICAgICAgICAgICAgIG1heFNpemUgPSBtYXgobWF4U2l6ZSwgZHBbaV1bal0pOwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChsZWZ0U3VtIDwgcmlnaHRTdW0pIHsKICAgICAgICAgICAgICAgICAgICBhKys7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIGItLTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIAogICAgY291dCA8PCBtYXhTaXplIDw8IGVuZGw7CiAgICByZXR1cm4gMDsKfQo=