let k = 3 let list = [|7; 18; 6; 10; 12; 128|] printfn "%A" list let mutable cache = Map.empty for i in list.Length - 1 .. -1 .. 0 do if i >= list.Length - k - 1 then cache <- cache.Add(i, (list.[i], [i])) else let mutable maxSum = System.Int32.MinValue let mutable maxList = List.empty for j in k .. min (2*k) (list.Length - i - 1) do let tSum, tList = cache.[i + j] if tSum > maxSum then maxSum <- tSum maxList <- tList cache <- cache.Add(i, (list.[i] + maxSum, i :: maxList)) let mutable maxSum = System.Int32.MinValue let mutable maxList = List.empty for j in 0 .. k do let tSum, tList = cache.[j] if tSum > maxSum then maxSum <- tSum maxList <- tList printfn "%i: %A" maxSum maxList