#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> riceBalls;
vector<int> prefixSum;
vector<vector<int>> memo;
// Helper function to calculate sum of rice balls from index i to j
int getSum(int i, int j) {
return prefixSum[j + 1] - prefixSum[i];
}
// Recursive function to find the maximum rice ball size that can be formed from subarray [i, j]
int maxRiceBall(int i, int j) {
if (i == j) return riceBalls[i]; // Single rice ball case
if (memo[i][j] != -1) return memo[i][j]; // Return memoized result if available
int maxBall = 0;
// Try merging adjacent sections
for (int mid = i; mid < j; ++mid) {
if (maxRiceBall(i, mid) == maxRiceBall(mid + 1, j)) {
maxBall = max(maxBall, maxRiceBall(i, mid) + maxRiceBall(mid + 1, j));
}
}
// Try merging with one rice ball in between
for (int mid = i; mid < j - 1; ++mid) {
int leftSum = getSum(i, mid);
int rightSum = getSum(mid + 2, j);
if (leftSum == rightSum) {
int leftMax = maxRiceBall(i, mid);
int rightMax = maxRiceBall(mid + 2, j);
if (leftMax > 0 && rightMax > 0) {
maxBall = max(maxBall, leftSum + riceBalls[mid + 1] + rightSum);
}
}
}
// Store and return the maximum size for the subarray [i, j]
memo[i][j] = max(maxBall, getSum(i, j)); // Consider the option of not merging at all
return memo[i][j];
}
int main() {
int N;
cin >> N;
riceBalls.resize(N);
prefixSum.resize(N + 1, 0);
memo.assign(N, vector<int>(N, -1));
for (int i = 0; i < N; ++i) {
cin >> riceBalls[i];
prefixSum[i + 1] = prefixSum[i] + riceBalls[i];
}
// Calculate the largest rice ball possible
int maxRiceBallSize = 0;
for (int i = 0; i < N; ++i) {
for (int j = i; j < N; ++j) {
maxRiceBallSize = max(maxRiceBallSize, maxRiceBall(i, j));
}
}
cout << maxRiceBallSize << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdmVjdG9yPGludD4gcmljZUJhbGxzOwp2ZWN0b3I8aW50Pi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V0U3VtKGksIG1pZCk7CiAgICAgICAgaW50IHJpZ2h0U3VtID0gZ2V0U3VtKG1pZCArIDIsIGopOwoKICAgICAgICBpZiAobGVmdFN1bSA9PSByaWdodFN1bSkgewogICAgICAgICAgICBpbnQgbGVmdE1heCA9IG1heFJpY2VCYWxsKGksIG1pZCk7CiAgICAgICAgICAgIGludCByaWdodE1heCA9IG1heFJpY2VCYWxsKG1pZCArIDIsIGopOwogICAgICAgICAgICBpZiAobGVmdE1heCA+IDAgJiYgcmlnaHRNYXggPiAwKSB7CiAgICAgICAgICAgICAgICBtYXhCYWxsID0gbWF4KG1heEJhbGwsIGxlZnRTdW0gKyByaWNlQmFsbHNbbWlkICsgMV0gKyByaWdodFN1bSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgLy8gU3RvcmUgYW5kIHJldHVybiB0aGUgbWF4aW11bSBzaXplIGZvciB0aGUgc3ViYXJyYXkgW2ksIGpdCiAgICBtZW1vW2ldW2pdID0gbWF4KG1heEJhbGwsIGdldFN1bShpLCBqKSk7IC8vIENvbnNpZGVyIHRoZSBvcHRpb24gb2Ygbm90IG1lcmdpbmcgYXQgYWxsCiAgICByZXR1cm4gbWVtb1tpXVtqXTsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgTjsKICAgIGNpbiA+PiBOOwogICAgcmljZUJhbGxzLnJlc2l6ZShOKTsKICAgIHByZWZpeFN1bS5yZXNpemUoTiArIDEsIDApOwogICAgbWVtby5hc3NpZ24oTiwgdmVjdG9yPGludD4oTiwgLTEpKTsKCiAgICBmb3IgKGludCBpID0gMDsgaSA8IE47ICsraSkgewogICAgICAgIGNpbiA+PiByaWNlQmFsbHNbaV07CiAgICAgICAgcHJlZml4U3VtW2kgKyAxXSA9IHByZWZpeFN1bVtpXSArIHJpY2VCYWxsc1tpXTsKICAgIH0KCiAgICAvLyBDYWxjdWxhdGUgdGhlIGxhcmdlc3QgcmljZSBiYWxsIHBvc3NpYmxlCiAgICBpbnQgb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