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 <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() - 1) {
                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;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int N;cin>>N;
        if(N==0) break;
        string S;cin>>S;
        int M=int(S.size());
        int maxi=-1,cnt=0;
        vector<int> ans;
        
        for(int bit=0;bit<(1<<(M-1));bit++){
            int now=0;
            int sum=0;
            vector<int> use;
            for(int i=0;i<M;i++){
                now*=10;
                now+=S[i]-'0';
                
                if(!(bit&(1<<i))){
                    sum+=now;
                    use.push_back(now);
                    now=0;
                }
            }
            
            if(sum<=N){
                if(sum==maxi) cnt++;
                else if(sum>maxi){
                    maxi=sum;
                    cnt=1;
                    ans=use;
                }
            }
        }
        
        if(maxi==-1) cout<<"error"<<endl;
        else if(cnt>=2) cout<<"rejected"<<endl;
        else{
            cout<<maxi;
            for(int a:ans) cout<<" "<<a;
            cout<<endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

#define REP(i,a,b) for(int i=int(a);i<int(b);i++)

using namespace std;

typedef long long int lli;

int main () {
    int t;
    string num;
    while (cin >> t >> num, t != 0 || num != "0") {
        int ans = -1;
        int cnt = 0;
        vector<int> nl;
        if (t == atoi(num.c_str())) {
            cout << t << ' ' << t << endl;
            continue;
        }
        REP (i, 0, num.size() - 1) {
            vector<int> dis(num.size() - 1, false);
            for (int j = dis.size() - 1; j >= int(dis.size() - i - 1); j--) dis[j] = true;
            do {
                int a = num[0] - '0';
                int sum = 0;
                bool mis = false;
                vector<int> prt;
                REP (k, 0, dis.size()) {
                    if (dis[k]) {
                        prt.push_back(a);
                        if (a == t) mis = true;
                        sum += a;
                        a = 0;
                    }
                    a = a * 10 + num[k + 1] - '0';
                }
                prt.push_back(a);
                sum += a;
                if (!mis && sum <= t) {
                    if (ans == sum) {
                        cnt++;
                    } else if (ans < sum){
                        cnt = 1;
                        ans = sum;
                        nl = prt;
                    }
                }
            } while (next_permutation(dis.begin(), dis.end()));
        }
        if (cnt >= 2) {
            cout << "rejected" << endl;
        } else if (ans == -1) {
            cout << "error" << endl;
        } else {
            cout << ans;
            for (int a : nl) {
                cout << ' ' << a;
            }
            cout << endl;
        }
    }
    return 0;
}