#include <iostream>
#include <vector>
#include <climits>
using namespace std;
long long maxCrossingSum(const vector<int>& nums, int l, int r, int midVal) {
// 左半部分(<= midVal)的最大後綴和
long long leftSum = LLONG_MIN, sum = 0;
for (int i = l; i <= r; ++i) {
if (nums[i] > midVal) break; // 超出當前值域範圍
sum += nums[i];
leftSum = max(leftSum, sum);
}
// 右半部分(> midVal)的最大前綴和
long long rightSum = LLONG_MIN;
sum = 0;
for (int i = r; i >= l; --i) {
if (nums[i] <= midVal) break; // 超出當前值域範圍
sum += nums[i];
rightSum = max(rightSum, sum);
}
// 跨越中值的和
long long crossSum = (leftSum != LLONG_MIN ? leftSum : 0) +
(rightSum != LLONG_MIN ? rightSum : 0);
return max(leftSum, max(rightSum, crossSum));
}
long long valueDivide(const vector<int>& nums, int l, int r, int minVal, int maxVal) {
if (l == r) return nums[l];
if (minVal == maxVal) {
// 所有值相同,直接求和
long long sum = 0;
for (int i = l; i <= r; ++i) sum += nums[i];
return sum;
}
int midVal = minVal + (maxVal - minVal) / 2;
// 劃分值域
vector<int> left, right;
for (int i = l; i <= r; ++i) {
if (nums[i] <= midVal) left.push_back(nums[i]);
else right.push_back(nums[i]);
}
// 遞歸處理左右值域
long long leftSum = left.empty() ? LLONG_MIN : valueDivide(left, 0, left.size()-1, minVal, midVal);
long long rightSum = right.empty() ? LLONG_MIN : valueDivide(right, 0, right.size()-1, midVal+1, maxVal);
// 計算跨越中值的和
long long crossSum = maxCrossingSum(nums, l, r, midVal);
return max(leftSum, max(rightSum, crossSum));
}
int main() {
int n;
cin >> n;
vector<int> nums(n);
int minVal = INT_MAX, maxVal = INT_MIN;
for (int i = 0; i < n; ++i) {
cin >> nums[i];
minVal = min(minVal, nums[i]);
maxVal = max(maxVal, nums[i]);
}
cout << valueDivide(nums, 0, n-1, minVal, maxVal) << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y2xpbWl0cz4KdXNpbmcgb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jvc3NTdW0pKTsKfQoKbG9uZyBsb25nIHZhbHVlRGl2aWRlKGNvbnN0IHZlY3RvcjxpbnQ+JiBudW1zLCBpbnQgbCwgaW50IHIsIGludCBtaW5WYWwsIGludCBtYXhWYWwpIHsKICAgIGlmIChsID09IHIpIHJldHVybiBudW1zW2xdOwogICAgaWYgKG1pblZhbCA9PSBtYXhWYWwpIHsKICAgICAgICAvLyDmiYDmnInlgLznm7jlkIzvvIznm7TmjqXmsYLlkowKICAgICAgICBsb25nIGxvbmcgc3VtID0gMDsKICAgICAgICBmb3IgKGludCBpID0gbDsgaSA8PSByOyArK2kpIHN1bSArPSBudW1zW2ldOwogICAgICAgIHJldHVybiBzdW07CiAgICB9CiAgICAKICAgIGludCBtaWRWYWwgPSBtaW5WYWwgKyAobWF4VmFsIC0gbWluVmFsKSAvIDI7CiAgICAKICAgIC8vIOWKg+WIhuWAvOWfnwogICAgdmVjdG9yPGludD4gbGVmdCwgcmlnaHQ7CiAgICBmb3IgKGludCBpID0gbDsgaSA8PSByOyArK2kpIHsKICAgICAgICBpZiAobnVtc1tpXSA8PSBtaWRWYWwpIGxlZnQucHVzaF9iYWNrKG51bXNbaV0pOwogICAgICAgIGVsc2UgcmlnaHQucHVzaF9iYWNrKG51bXNbaV0pOwogICAgfQogICAgCiAgICAvLyDpgZ7mrbjomZXnkIblt6blj7PlgLzln58KICAgIGxvbmcgbG9uZyBsZWZ0U3VtID0gbGVmdC5lbXB0eSgpID8gTExPTkdfTUlOIDogdmFsdWVEaXZpZGUobGVmdCwgMCwgbGVmdC5zaXplKCktMSwgbWluVmFsLCBtaWRWYWwpOwogICAgbG9uZyBsb25nIHJpZ2h0U3VtID0gcmlnaHQuZW1wdHkoKSA/IExMT05HX01JTiA6IHZhbHVlRGl2aWRlKHJpZ2h0LCAwLCByaWdodC5zaXplKCktMSwgbWlkVmFsKzEsIG1heFZhbCk7CiAgICAKICAgIC8vIOioiOeul+i3qOi2iuS4reWAvOeahOWSjAogICAgbG9uZy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