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 <sstream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

string s; //yツづ個閉カツ篠堋療アツ表ツ記
int x,y,len;
int ans[6],ansSize,ansSum,ansFlg;
int tmp[6],size;

void solve(int rem,int idx,int sum){
	if(rem == 0 && idx == len){
		if(ansSum < sum && sum <= x){
			for(int i=0;i<size;i++) ans[i] = tmp[i];
			ansSize = size;
			ansSum = sum;
			ansFlg = 2;
		}
		else if(ansSum == sum) ansFlg = 1;
		return;
	}
	if(rem <= 0 || idx >= len) return;

	for(int nidx=idx+1;nidx<=len;nidx++){
		string sub = s.substr(idx,nidx-idx);
		int plus = atoi(&sub[0]);
		if(sum + plus <= x){
			tmp[size-rem] = plus;
			solve(rem-1,nidx,sum+plus);
		}
	}
}

string itos(int x){stringstream ss; ss<<x; return ss.str();}

int main(void){
	while(cin>>x>>y,x||y){
		s = itos(y);
		len = s.length();
		ansFlg = size = 0;
		ansSum = -1;

		//ツ閉ェツ環青板づ個個按津ィ
		for(size=1;size<=len;size++){
			solve(size,0,0);
		}

		if(ansFlg == 0)
			cout<<"error\n";
		else if(ansFlg == 1)
			cout<<"rejected\n";
		else{
			cout<<ansSum;
			for(int i=0;i<ansSize;i++) cout<<" "<<ans[i];
			cout<<endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
 
using namespace std;
 
#define INF 1e9
 
int t,ans,size,diff;
vector<int> a,path;
map<int,int> mp;
 
int calc(int idx,int N){
    int res = 0, p = 1;
    for(int i = N-1 ; i >= idx ; i--){
        res += a[i]*p;
        p *= 10;
    }
    return res;
}
 
void solve(int idx,int val,vector<int> &v){
    if(idx == size){
        if(val > t) return;
        if(t-val <= diff){
            diff = t-val;
            ans = val; mp[ans]++;
            path = v;
        }
        return;
    }
    for(int i = 1 ; i <= size ; i++){
        if(idx + i > size) continue;
        int c = calc(idx,idx+i);
        v.push_back(c);
        solve(idx+i,val+c,v);
        v.pop_back();
    }
}
 
int main(){
    int num;
    while(true){
        cin >> t >> num;
        if(t == 0 && num == 0) break;
        mp.clear(); a.clear(); 
        diff = ans = INF;
        for(int i = 0 ; ; i++){
            if(num == 0) break;
            a.push_back(num%10);
            num /= 10;
        }
        reverse(a.begin(),a.end());
        size = a.size();
        vector<int> v;
        solve(0,0,v);
        if(diff == INF){
            cout << "error" << endl;
        }else{
            if(mp[ans] != 1){
                cout << "rejected" << endl;
            }else{
                cout << ans;
                for(int i = 0 ; i < (int)path.size() ; i++){
                    cout << " " << path[i];
                }
                cout << endl;
            }
        }
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int T, P;
string N;
vector<int> ans;
bool dbld = false;

void show_vector(vector<int> v) {
    for(auto e : v) cout << e << " ";
    cout << endl;
}

void solve(int p, vector<int> v) {
    if(p == N.length()) {
        int sum = 0;
        rep(i,0,v.size()) sum += v[i];
        if(T >= sum) {
            if(P < sum) {
                P = sum;
                ans = v;
                dbld = false;
            }
            else if(P == sum) {
                // show_vector(v);
                dbld = true;
            }
        }
    }
    else {
        int val = N[p] - '0';
        int idx = v.size() - 1;
        vector<int> s = v, t = v;
        s[idx] *= 10; s[idx] += val;
        t.push_back(val);
        solve(p+1, s);
        if(p != 0) solve(p+1, t);
    }
}

signed main() {
    while(cin >> T >> N, T) {
        P = -1; dbld = false;

        vector<int> INIT(1, 0);
        solve(0, INIT);

        if(P == -1) cout << "error" << endl;
        else if(dbld) cout << "rejected" << endl;
        else {
            printf("%lld", P);
            rep(i,0,ans.size()) printf(" %lld", ans[i]);
            cout << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    while(1){
        int t; cin >> t;
        string s; cin >> s;
        if(t==0&&s=="0")break;
        int p=0;
        for(char c:s){
            p+=c-'0';
        }
        if(p>t){
            printf("error\n");
            continue;
        }
        p=-1;
        int cnt=1;
        int n=s.size();
        vector<int> res;
        vector<int> v;
        for(int i=0;i<(1<<n-1);i++){
            v.clear();
            int sum=0;
            int now=s[0]-'0';
            for(int j=0;j<n-1;j++){
                if((i>>j)&1){
                    v.push_back(now);
                    sum+=now;
                    now=s[j+1]-'0';
                }
                else{
                    now=now*10+s[j+1]-'0';
                }
            }
            sum+=now;
            v.push_back(now);
            if(sum<=t){
                if(p==sum){
                    cnt++;
                }
                else if(sum>p){
                    p=sum;
                    cnt=1;
                    res=v;
                }
            }
        }
        if(cnt>=2){
            printf("rejected\n");
        }
        else{
            printf("%d",p);
            for(int vv:res){
                printf(" %d",vv);
            }
            printf("\n");
        }
    }
}