from functools import cache

def decompose(n):
    if n == 0:
        return [[]]
    elif n == 1:
        return [[1]]
    ans = []
    for k in range(1, n + 1):
        for d in decompose(n - k):
            ans.append([k] + d[:])
    return ans

def get_sum(xs, d):
    k, s = 0, 0
    for x in d:
        s += int(''.join(map(str, xs[k: k + x])))
        k += x
    return s

    
def cut_number(n, t):
    ans = -1
    xs = [int(c) for c in str(n)]
    m = len(xs)
    for d in decompose(m):        
        s = get_sum(xs, d)
        if s <= t:
            ans = max(ans, s)
    return ans

        
n = 1234
t = 20
print(cut_number(n, t))
        
    

Embed on website

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