Go Max Double Slice Sum
func maxDoubleSliceSum(a []int) int {
size := len(a)
if size < 3 {
return 0
}
p1 := make([]int, size)
p2 := make([]int, size)
p1[1] = 0
p2[size-2] = 0
for i := 2; i < size-1; i++ {
if v := p1[i-1] + a[i-1]; v > 0 {
p1[i] = v
}
if v := p2[size-i] + a[size-i]; v > 0 {
p2[size-i-1] = v
}
}
sum := p1[1] + p2[1]
for i := 1; i < size-1; i++ {
if s := p1[i] + p2[i]; s > sum {
sum = s
}
}
return sum
}
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.