How to cut a list by max sum
Split
ClearAll[split]
split[lst_, maxsum_] := Module[{s = lst[[1]]},
Split[lst, Or[(s+= #2) <= maxsum, s = #2] &]]
Examples:
split[{5, 10, 3, 8, 9, 8, 8, 4, 1, 7}, 20]
{{5, 10, 3}, {8, 9}, {8, 8, 4}, {1, 7}}
split[{1, 2, 3, 4, 6, 4, 3}, 20]
{{1, 2, 3, 4, 6, 4}, {3}}
SequenceSplit
ClearAll[seqSplit]
seqSplit = SequenceSplit[#, a : {__} /; Total[a] <= #2 :> a] &;
seqSplit[{5, 10, 3, 8, 9, 8, 8, 4, 1, 7}, 20]
{{5, 10, 3}, {8, 9}, {8, 8, 4}, {1, 7}}
seqSplit[{1, 2, 3, 4, 6, 4, 3}, 20]
{{1, 2, 3, 4, 6, 4}, {3}}
Reap + Sow
ClearAll[reapSow]
reapSow[lst_, maxsum_] := Module[{i = 0, s = 0},
Last @ Reap[Scan[Sow[#, If[(s += #) <= maxsum, i, s = #; ++i]] &, lst]]]
reapSow[{5, 10, 3, 8, 9, 8, 8, 4, 1, 7}, 20]
{{5, 10, 3}, {8, 9}, {8, 8, 4}, {1, 7}}
reapSow[{1, 2, 3, 4, 6, 4, 3}, 20]
{{1, 2, 3, 4, 6, 4}, {3}}
ReplaceRepeated + TakeDrop + Accumulate
takeDrop[lst_, maxsum_] := {lst} //. {a___List, b_List} /; Length[b] > 1 :>
{a, ## & @@ TakeDrop[b, LengthWhile[Accumulate[b], # <= maxsum &]]} //
DeleteCases[{}]
takeDrop[{5, 10, 3, 8, 9, 8, 8, 4, 1, 7}, 20]
{{5, 10, 3}, {8, 9}, {8, 8, 4}, {1, 7}}
takeDrop[{1, 2, 3, 4, 6, 4, 3}, 20]
{{1, 2, 3, 4, 6, 4}, {3}}