1. JavaScript
function solution(A) {
const N = A.length;
if(N === 3) return 0;
let start = Array(N).fill(0);
let tail = Array(N).fill(0);
for(let i = 1; i < N-1; i++) {
start[i] = Math.max(0, start[i-1] + A[i]);
}
for(let i = N-2; i >= 1; i--) {
tail[i] = Math.max(0, tail[i+1] + A[i]);
}
let max = 0;
for(let i = 1; i < N-1; i++) {
max = Math.max(max, start[i-1] + tail[i+1]);
}
return max;
}
2. Python
def solution(A):
length = len(A)
start = [0] * length
end = [0] * length
for i in range(1, length - 1):
start[i] = max(0, start[i-1] + A[i])
for i in range(length - 2, 0, -1):
end[i] = max(0, end[i + 1] + A[i])
answer = 0
for i in range(1, length - 1):
answer = max(answer, start[i-1] + end[i+1])
return answer