fork(2) download
  1. let k = 3
  2. let list = [|7; 18; 6; 10; 12; 128|]
  3. printfn "%A" list
  4.  
  5. let mutable cache = Map.empty
  6.  
  7. for i in list.Length - 1 .. -1 .. 0 do
  8. if i >= list.Length - k - 1 then
  9. cache <- cache.Add(i, (list.[i], [i]))
  10. else
  11. let mutable maxSum = System.Int32.MinValue
  12. let mutable maxList = List.empty
  13. for j in k .. min (2*k) (list.Length - i - 1) do
  14. let tSum, tList = cache.[i + j]
  15. if tSum > maxSum then
  16. maxSum <- tSum
  17. maxList <- tList
  18. cache <- cache.Add(i, (list.[i] + maxSum, i :: maxList))
  19.  
  20. let mutable maxSum = System.Int32.MinValue
  21. let mutable maxList = List.empty
  22. for j in 0 .. k do
  23. let tSum, tList = cache.[j]
  24. if tSum > maxSum then
  25. maxSum <- tSum
  26. maxList <- tList
  27. printfn "%i: %A" maxSum maxList
Success #stdin #stdout 0.11s 24284KB
stdin
Standard input is empty
stdout
[|7; 18; 6; 10; 12; 128|]
146: [1; 5]