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 <bits/stdc++.h>

using namespace std;

vector<int> calc(int t, string &num, int key){
	vector<int> sum;
	string::reverse_iterator it = num.rbegin();
	int temp = *it - '0';
	int mlt = 10;
	it++;
	for(int i = 1; i <= 1 << (num.size() -2); i <<= 1){
		if((key & i) == i){
			sum.push_back(temp);
			temp = *it - '0';
			mlt = 10;
		}
		else{
			temp += (*it - '0')*mlt;
			mlt *= 10;
		}
		it++;
	}
	sum.push_back(temp);
	/*
	for(int i = 0; i < sum.size(); i++){
		cout << sum[i] << " ";
	}
	cout << endl;
	*/
	return sum;
}

int main(){
	while(true){
		int t;
		string num;
		cin >> t >> num;
		if(t == 0 )break;
		vector<int> ans(1);
		ans[0] = atoi(num.c_str());
		int anssum = t >= ans[0] ? ans[0] : 0;
		bool flag = false;
		for(int i = 1; i < (1 << (num.size()-1)); i++){
			vector<int> temp = calc(t,num,i);
			int sum = accumulate(temp.begin(),temp.end(),0);
			if(sum > t)continue;
			if(sum > anssum){
				anssum = sum;
				swap(ans,temp);
				flag = false;
			}
			else if(sum == anssum){
				flag = true;
			}
		}
		if(anssum == 0){
			cout << "error" << endl;
		}
		else if(flag){
			cout << "rejected" << endl;
		}
		else {
			cout << anssum << " ";
			for(int i = ans.size()-1; i >= 0; i--){
				cout << ans[i];
				if(i != 0){
					cout << " ";
				}
			}
			cout << endl;
		}
	}
		
	return 0;
}

C++: #include <bits/stdc++.h>
 
#define loop(n, i) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

int main()
{
    while (1) {
        int t;
        string num;
        cin >> t >> num;
        if (!t && num == "0") break;

        int len = num.length();
        int as = -1;
        int r = 0;
        vector<int> av;
        loop (1<<(len-1), i) {
            string s = num;
            vector<int> n;
            int idx = 0;
            loop (len-1, j) {
                if ((i>>j) & 1) {
                    string q = num.substr(idx, j+1-idx);
                    n.push_back(stoi(q));
                    idx = j+1;
                }
            }
            n.push_back(stoi(num.substr(idx)));
            int sum = 0;
            for (int v : n) sum += v;
            if (t >= sum && sum > as) {
                as = sum;
                av = n;
                r = 0;
            } else if (sum == as) {
                r = 1;
            }
        }
        if (as == -1) {
            cout << "error" << endl;
        } else if (r) {
            cout << "rejected" << endl;
        } else {
            cout << as;
            for (int v : av) cout << " " << v;
            cout << endl;
        }
    }
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <sstream>
#include <deque>

#define REP(i, n) for ( int i = 0; i < n; i++ )
#define NIL -1

using namespace std;

int N, maxVal;
vector<vector<int> > ans;

int getFigure(int num) {
    if(num == 0) return 1;

    int ret = 0;
    while(num > 0) {
        num /= 10;
        ret++;
    }
    return ret;
}

void merge(vector<int> &nums, int idx) {
    int nonZero = 0,
        prevIdx = 0;
    REP(i, nums.size()) {
        if(nums[i] == NIL) continue;
        nonZero++;

        if(nonZero == idx + 2 && nums[prevIdx] != 0) {
            int step = getFigure(nums[i]);
            REP(j, step) nums[prevIdx] *= 10;
            nums[prevIdx] += nums[i];
            nums[i] = NIL;
            break;
        }
        prevIdx = i;
    }
}

int sum(vector<int> &nums) {
    int ret = 0;
    REP(i, nums.size()) if(nums[i]!=NIL) ret += nums[i];
    return ret;
}

void dump(vector<int> &nums) {
    REP(i, nums.size()) cout << nums[i] << ",";
    // REP(i, nums.size()) if(nums[i]!=NIL)cout << nums[i] << ",";
    cout << endl;
}

bool equal(vector<int> &a, vector<int> &b) {
    int ai = 0, bi = 0;
    REP(i, a.size()) {
        while(a[ai] == 0 && ai < a.size()) ai++;
        while(b[bi] == 0 && bi < b.size()) bi++;
        if(a[ai] != b[bi]) return 0;
        ai++; bi++;
    }
    return 1;
}

bool valid(vector<int> &nums) {
    int cnt = 0;
    REP(i, nums.size()) if(nums[i] != NIL) cnt += getFigure(nums[i]);
    return cnt == nums.size();
}

void solve(vector<int> &nums, int d = 0, int oft = 0) {
    // cout << d << ": ";
    // dump(nums);

    int s = sum(nums);
    vector<int> base = nums;

    if(s > N) return;

    if (s > maxVal) {
        ans.clear();
        ans.push_back(nums);

        maxVal = s;

    } else if(s == maxVal && !equal(ans[0], nums)) {
        ans.push_back(nums);
    }

    for(int i = oft; i < nums.size() - (d+1); i++) {
    // REP(i, nums.size() - (d+1)) {
        merge(nums, i);
        solve(nums, d+1, oft);
        nums = base;
    }
}

int main() {
    int num;
    vector<int> nums;

    while(cin >> N >> num, N||num) {

        maxVal = -1;
        ans.clear();
        nums.clear();

        while(num > 0) {
            nums.push_back(num % 10);
            num /= 10;
        }
        reverse(nums.begin(), nums.end());

        solve(nums);

        // cout << ans.size() << endl;

        // NOTE: どこかの初期化をミスっててとある入力の組み合わせだと同じ答えが入ってしまう
        int dup = 0;
        int containZero = 0;
        if(ans.size() > 1) {
            REP(i, ans.size()-1) {
                if(equal(ans[i], ans[i+1])) dup++;
            }
        } else {
            REP(i, ans[0].size()) {
                if(ans[0][i] == 0) containZero = 1;
            }
        }

        if(ans.size() - dup == 0) {
            cout << "error" << endl;
        } else if (ans.size() - dup >= 2 || containZero) {
            cout << "rejected" << endl;
        } else {
            cout << sum(ans[0]);
            REP(i, ans[0].size()) if(ans[0][i] != NIL) cout << " " << ans[0][i];
            cout << endl;
        }
    }

    return 0;
}