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, " "))
		}
	}
}

C++: #include <iostream>
#include <string>
#include <vector>

using namespace std;

unsigned short dp[7][1000000];
unsigned char pr[7][1000000];
int df[7][1000000];

int main(){
	while(true){
		int n;
		string s;
		cin >> n >> s;
		if(n == 0 && s == "0"){ break; }
		const int m = s.size();
		for(int i = 0; i <= m; ++i){
			for(int j = 0; j <= n; ++j){
				dp[i][j] = pr[i][j] = df[i][j] = 0;
			}
		}
		dp[0][0] = 1;
		for(int i = 0; i < m; ++i){
			int x = 0;
			for(int j = i; j < m; ++j){
				x = (x * 10) + s[j] - '0';
				if(x > n){ break; }
				for(int k = 0; k + x <= n; ++k){
					if(dp[i][k] == 0){ continue; }
					dp[j + 1][k + x] += dp[i][k];
					pr[j + 1][k + x] = i;
					df[j + 1][k + x] = x;
				}
			}
		}
		int answer = -1;
		for(int i = n; answer < 0 && i >= 0; --i){
			if(dp[m][i] != 0){ answer = i; }
		}
		if(answer < 0){
			cout << "error" << endl;
		}else if(dp[m][answer] >= 2){
			cout << "rejected" << endl;
		}else{
			cout << answer;
			vector<int> cut;
			int r = m, c = answer;
			do {
				const int d = df[r][c];
				cut.push_back(d);
				r = pr[r][c];
				c -= d;
			} while(r > 0);
			for(int i = cut.size() - 1; i >= 0; --i){
				cout << " " << cut[i];
			}
			cout << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<functional>
using namespace std;

#define MAX_N 19

long long K, m;
int dig[MAX_N], cut[MAX_N], sum;
vector<pair<long long, string>>vec;
char T[11] = "0123456789";

void _memset() {
	sum = 0;
	for (int i = 0; i < MAX_N; i++) {
		dig[i] = 0;
		cut[i] = 0;
	}
	vec.clear();
}

long long power(long long n, long long v) {
	long long res = 1;
	for (int i = 0; i < v; i++) {
		res *= n;
	}
	return res;
}

int digits(long long n) {
	for (int i = 0; i < MAX_N; i++) {
		if (power(10, i) >= n) { return i + 1; }
	}
}

long long STOI(string U) {
	long long res = 0;
	for (int i = 0; i < U.size(); i++) {
		for (int j = 0; j < 10; j++) {
			if (U[i] == T[j]) {
				res += power(10, U.size() - 1 - i)*j;
			}
		}
	}
	return res;
}

string TOSTR(long long n) {
	string S = ""; bool ok = false;
	for (int i = MAX_N - 1; i >= 0; i--) {
		long long b = power(10, i);
		long long c = (n / b) % 10;
		if (c >= 1) {
			ok = true;
		}
		if (ok == true) {
			S += T[c];
		}
	}
	return S;
}

string make(vector<long long>vect) {
	string W = "";
	for (int i = 0; i < vect.size(); i++) {
		if (i >= 1) { W += ','; }
		W += TOSTR(vect[i]);
	}
	return W;
}

vector<long long>kaiseki(string W) {
	vector<long long>vec4;
	string X = "";
	W += ',';
	for (int i = 0; i < W.size(); i++) {
		if (W[i] == ',') {
			vec4.push_back(STOI(X));
			X = "";
		}
		else {
			X += W[i];
		}
	}
	return vec4;
}

int main() {
	while (true) {
		_memset();
		cin >> K >> m;
		if (K == 0 && m == 0) {
			break;
		}

		int digit = digits(m);
		string S = "";
		vector<long long>vec2;
		vec2.clear();

		for (int i = 0; i < MAX_N; i++) {
			dig[i] = (m / power(10, i)) % 10;
		}
		for (int i = 0; i < power(2, digit - 1); i++) {
			S = "";
			sum = 0;
			vec2.clear();
			for (int j = 0; j < MAX_N; j++) {
				cut[j] = 0;
			}
			for (int j = 0; j < digit - 1; j++)
			{
				int a = power(2, j);
				cut[j] = (i / a) % 2;
			}

			for (int j = digit - 1; j >= 1; j--) {
				S += T[dig[j]];
				if (cut[j - 1] == 1) {
					vec2.push_back(STOI(S));
					sum += vec2[vec2.size() - 1];
					S = "";
				}
			}
			S += T[dig[0]];
			vec2.push_back(STOI(S));
			sum += vec2[vec2.size() - 1];
			S = "";

			if (sum <= K && vec2[0] != 0) {
				vec.push_back(make_pair(sum, make(vec2)));
			}
		}
		sort(vec.begin(), vec.end(),greater<pair<long long, string>>());

		long long target;
		int sum2 = 0;

		if (vec.size() >= 1) {
			sum2 = 0;
			target = vec[0].first;
			for (int i = 0; i < vec.size(); i++) {
				if (vec[i].first == target) {
					sum2++;
				}
				else {
					break;
				}
			}
		}
		if (sum2 == 0) {
			cout << "error" << endl;
		}
		if (sum2 >= 2) {
			cout << "rejected" << endl;
		}
		if (sum2 == 1) {
			cout << vec[0].first << ' ';
			vector<long long>vec3 = kaiseki(vec[0].second);
			for (int i = 0; i < vec3.size(); i++) {
				if (i >= 1) { cout << ' '; }
				cout << vec3[i];
			}
			cout << endl;
		}

	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t, n;
    while(cin >> t >> n, t, n){
        string s = to_string(n);
        int l = s.size();
        int MAX = -1;
        vector<int> ans;
        bool f = false;
        rep(i, 0, (1LL << (l - 1))){
            int now = s[0] - '0';
            int sum = 0;
            vector<int> tmp;
            rep(j, 0, l - 1){
                if(i & (1LL << j)){
                    tmp.push_back(now);
                    sum += now;
                    now = s[j + 1] - '0';
                }else{
                    now = now * 10 + s[j + 1] - '0';
                }
            }
            tmp.push_back(now);
            sum += now;
            if(sum <= t && MAX < sum){
                MAX = sum;
                ans = tmp;
                f = true;
            }else if(sum <= t && MAX == sum){
                f = false;
            }
        }
        if(MAX == -1) cout << "error" << endl;
        else if(f == false) cout << "rejected" << endl;
        else{
            cout << MAX << " ";
            for(int ii = 0; ii < ans.size(); ii++){
                if(ii) cout << ' ';
                cout << ans[ii];
            }
            cout << endl;
        }
    }
}