xs = sorted(list(map(int, input().split())), reverse=True)
n = int(input())
print(xs)
def solve(n):
    q = [(n, [])]
    seen = set()
    ans = []
    while len(q) > 0:

        e, p = q.pop(0)
        if e == 0:
            ans.append(tuple(p))
        for i, x in enumerate(xs):
            a, b = divmod(e, x)
            if a > 0:
                _p = tuple(p + [(x, a)])
                if not _p in seen:
                    seen.add(_p)
                    q.append((b, list(_p)))
                if a > 1:
                    _q = tuple(p + [(x, a - 1)])
                    if not _q in seen:
                        seen.add(_q)
                        q.append((b + x, list(_q)))

    res = max(ans, key=lambda x: (-sum([y[1] for y in x]), [y[0] for y in x]))
    # print(ans)
    return res
r = solve(n)
if len(r) == 0:
    print("0")
else:
    print(' '.join(' '.join([str(x)] * y) for x, y in r))

coins = [int(s) for s in input().split()]
coins.sort(reverse = True)
goal_amount = int(input())

# Top rated solution
# changes = {0: [0]}
# changes.update({v: [v] for v in coins})
# actives = [0] + coins
# while goal_amount not in changes and min(actives) < goal_amount:
#     new_actives = []
#     for active in actives:
#         for coin in coins:
#             if active+coin not in changes:
#                 changes[active+coin] = changes[active] + [coin]
#                 new_actives.append(active+coin)
#     actives = new_actives

# change = changes.get(goal_amount)
# if change is None:
#     print('IMPOSSIBLE')
# else:
#     print(*change)

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: