Marson,
не самый оптимальный вариант ...
const maxSumm = arr => {
if(arr.length < 2) return Math.max(...arr, 0);
let temp = [[0, arr[1]], [arr[0], 0]],
up = [];
for (let i = 2; i < arr.length; i++) {
up = [];
temp = temp.map(ar => {
let last = ar[ar.length - 1],
prev = ar[ar.length - 2];
if (last) ar.push(0);
else if (prev) {
up.push([...ar, 0]);
ar.push(arr[i])
} else ar.push(arr[i])
return ar
});
temp = temp.concat(up)
}
temp = temp.map(a => a.reduce((a, b) => a + b));
return Math.max(...temp);
}
alert(maxSumm([4, 10, 3, 1, 5]))