Lisp Max Double Slice Sum
(defun max-double-slice-sum (a)
(let* ((vec (coerce a 'vector))
(size (length vec)))
(if (< size 3)
0
(let ((p1 (make-array size :initial-element 0))
(p2 (make-array size :initial-element 0)))
(setf (aref p1 1) 0)
(setf (aref p2 (- size 2)) 0)
(loop for i from 2 below (1- size)
do (progn
(setf (aref p1 i) (max 0 (+ (aref p1 (1- i)) (aref vec (1- i)))))
(setf (aref p2 (- size i 1)) (max 0 (+ (aref p2 (- size i)) (aref vec (- size i)))))))
(let ((sum (+ (aref p1 1) (aref p2 1))))
(loop for i from 1 below (1- size)
do (setf sum (max sum (+ (aref p1 i) (aref p2 i)))))
sum)))))
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.