Task

Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one.



An empty subsequence is considered to have the sum of   0;   thus if all elements are negative, the result must be the empty sequence.




greatest_subsequential_sum_it([]) = [] => true.
greatest_subsequential_sum_it(A) = Seq =>
    P = allcomb(A),
    Total = max([Tot : Tot=_T in P]),
    Seq1 = [],
    if Total > 0 then
       [B,E] = P.get(Total),
       Seq1 := [A[I] : I in B..E]
    else 
      Seq1 := []
    end,
    Seq = Seq1.
 
allcomb(A) = Comb => 
   Len = A.length,
   Comb = new_map([(sum([A[I]:I in B..E])=([B,E])) : B in 1..Len, E in B..Len]).