Go: // Package main provides
//
// File:  1237.go
// Author: ymiyamoto
//
// Created on Wed Jul 11 10:35:26 2018
//
package main

import (
	"fmt"
	"strconv"
	"strings"
)

func comb(ns []string, aux [][]string) [][]string {
	if len(ns) == 0 {
		return aux
	}
	v := ns[0]
	a := [][]string{}
	for _, au := range aux {
		tmp := make([]string, len(au))
		copy(tmp, au)
		tmp[len(tmp)-1] += v
		a = append(a, tmp)

		au = append(au, v)
		a = append(a, au)
	}

	return comb(ns[1:], a)
}

func main() {
	for {
		var d int
		var num string
		fmt.Scan(&d, &num)
		if d == 0 {
			break
		}

		var min int
		ns := strings.Split(num, "")
		for i := range ns {
			v, _ := strconv.Atoi(ns[i])
			min += v
		}

		var ans int
		var pat []string
		var hasSame bool
		for _, c := range comb(ns[1:], [][]string{{ns[0]}}) {
			var total int
			for _, v := range c {
				n, _ := strconv.Atoi(v)
				total += n
			}
			if ans == total {
				hasSame = true
			} else if ans < total && total <= d {
				ans = total
				pat = c
				hasSame = false
			}
		}

		if ans == 0 {
			fmt.Println("error")
		} else if hasSame {
			fmt.Println("rejected")
		} else {
			fmt.Println(ans, strings.Join(pat, " "))
		}
	}
}

Python: #!/usr/bin/env python3
while True:
    t, n = input().split()
    t = int(t)
    if t == 0:
        exit()
    L = len(n)
    m = 0
    parts = []
    f = False
    for b in range(1 << (L - 1)):
        c = int(n[0])
        s = 0
        li = []
        for k in range(L - 1):
            if b >> k & 1 == 1:
                s += c
                li.append(c)
                c = 0
            c = 10 * c + int(n[k + 1])
        s += c
        li.append(c)
        c = 0
        if s > t:
            continue
        if s >= m:
            f = (s == m)
            m = s
            parts = li
    if f:
        print('rejected')
    elif m == 0:
        print('error')
    else:
        print(m, *parts, sep=' ')

Python: #!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve(t,n):
    if t == n:
        print(t,n)
        return
    a = list(map(int,list(str(n))))
    s = sum(a)
    m = len(a)
    if s > t:
        print("error")
        return
    nm = m-1
    f = [0]*nm
    d = defaultdict(lambda : 0)
    d[tuple(f)] = 1
    l = [[] for i in range(t+1)]
    q = [(s,f)]
    l[s].append(f)
    ans = s
    while q:
        s,f = heappop(q)
        for i in range(nm):
            if f[i]:
                continue
            nf = [1 if j == i else f[j] for j in range(nm)]
            ns = 0
            ms = a[0]
            for j in range(1,m):
                if nf[j-1]:
                    ms *= 10
                    ms += a[j]
                else:
                    ns += ms
                    ms = a[j]
            ns += ms
            if ns <= t:
                tf = tuple(nf)
                if not d[tf]:
                    d[tf] = 1
                    if ns >= ans:
                        ans = ns
                        l[ns].append(nf)
                    heappush(q,(ns,nf))
    if len(l[ans]) > 1:
        print("rejected")
        return
    s = [ans]
    f = l[ans][0]
    k = a[0]
    for i in range(nm):
        if f[i]:
            k *= 10
            k += a[i+1]
        else:
            s.append(k)
            k = a[i+1]
    s.append(k)
    print(*s)
    return

#Solve
if __name__ == "__main__":
    while 1:
        t,n = LI()
        if t == n == 0:
            break
        solve(t,n)

Python: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []
    ii = [2**i for i in range(6)]

    while True:
        t,n = LI()
        if t == 0 and n == 0:
            break

        s = [c for c in str(n)]
        l = len(s)

        r = None
        rm = -1
        rc = 0
        for i in range(2**(l-1)):
            k = []
            c = s[0]
            for j in range(l-1):
                if ii[j] & i:
                    c += s[j+1]
                else:
                    k.append(c)
                    c = s[j+1]
            k.append(c)
            m = sum(map(int,k))
            if m <= t:
                if rm < m:
                    rm = m
                    r = k
                    rc = 1
                elif rm == m:
                    rc += 1
        if rc == 0:
            rr.append('error')
        elif rc > 1:
            rr.append('rejected')
        else:
            rr.append('{} {}'.format(rm, ' '.join(r)))

    return '\n'.join(map(str, rr))


print(main())

Python: import itertools
from heapq import heappop, heappush
from collections import defaultdict
def main(t, num):
    num = list(str(num))
    d = defaultdict(int)
    if t < sum(map(int, num)):
        print("error")
        return
    q = [[0,[]]]
    itertools.product()
    fulls = itertools.product(range(2), repeat=len(num) - 1)
    for full in fulls:
        tmp = 0
        res = []
        b = 0
        f = False
        for i in range(len(num) - 1):
            if full[i]:
                res.append(i)
                tmp += int("".join(num[b: i + 1]))
                b = i + 1
        tmp += int("".join(num[b:]))
        a, b = heappop(q)
        a *= - 1
        if a == tmp: d[a] += 1
        if t >= tmp > a:
            a = tmp
            b = res
        heappush(q, [-a, b])
    a, b = heappop(q)
    a *= - 1
    if d[a] or a == 0:
        print("rejected")
    else:
        ans = []
        be = 0
        for i in b:
            ans.append(int("".join(num[be: i + 1])))
            be = i + 1
        ans.append(int("".join(num[be:])))
        print(a,*ans)

if __name__ == "__main__":
    while 1:
        t, num = map(int, input().split())
        if t == num == 0:
            break
        main(t, num)