К моему "алгоритму" из комментариев, могу привести такой код на python:
(я видел, что вопрос с тегом "JAVASCRIPT", но лучше так, чем вовсе без кода)
from itertools import combinations
def ungroup(data_list, limit):
data_list.sort()
result_list = []
while sum(data_list) > limit:
series = [data_list.pop()]
rest = limit - series[0]
if rest == 0:
result_list.append(series)
elif rest in data_list:
result_list.append(series + [data_list.pop(data_list.index(rest))])
else:
sublist = data_list.copy()
for i in data_list:
if i > rest:
sublist = data_list[:data_list.index(i)]
break
for n in range(2, len(sublist)):
comb = {sum(i): i for i in combinations(sublist, n)}
if rest in comb.keys():
series.extend(comb[rest])
for i in comb[rest]: data_list.remove(i)
break
result_list.append(series)
if len(data_list) > 0:
result_list.append(data_list)
return result_list
print(ungroup([6, 4, 4, 3, 2, 1.5, 1, 0.5], 6))
# [[6], [4, 2], [4, 0.5, 1.5], [1, 3]]