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>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;

string s, ans_str;
int n, ans_num;
bool f[8];
map<int, int> m;

typedef string::const_iterator State;
int number(State&);
int factor(State&);
int term(State&);
int expression(State&);

// ??°??????????????????????????????????????°????????????
int number(State &begin) {
    int ret = 0;

    while (isdigit(*begin)) {
        ret *= 10;
        ret += *begin - '0';
        begin++;
    }

    return ret;
}

// ??¬??§?????°???????????????????????????????????????????????????
int factor(State &begin) {
    if (*begin == '(') {
        begin++; // '('????£???°??????
        int ret = expression(begin);
        begin++; // ')'????£???°??????
    } else {
        return number(begin);
    }
    return INF;
}

// ?????????????????????????????????????????????????????????????????????
int term(State &begin) {
    int ret = factor(begin);

    for (;;) {
        if (*begin == '*') {
            begin++;
            ret *= factor(begin);
        } else if (*begin == '/') {
            begin++;
            ret /= factor(begin);
        } else {
            break;
        }
    }

    return ret;
}

// ?????????????????????????????????????????????????????????????????????
int expression(State &begin) {
    int ret = term(begin);

    for (;;) {
        if (*begin == '+') {
            begin++;
            ret += term(begin);
        } else if (*begin == '-') {
            begin++;
            ret -= term(begin);
        } else {
            break;
        }
    }

    return ret;
}

void solve(string p, int i){
    if(i == s.size()){
        State it = p.begin();
        int temp = expression(it);
        if(abs(n - ans_num) >= abs(n - temp) && n >= temp){
            ans_str = p;
            ans_num = temp;
            m[temp]++;
        }
        return;
    }

    p+=s[i];
    solve(p, i + 1);
    if(i + 1 != s.size() && f[i] == 0 && f[i + 1] == 0){
        p+='+';
        solve(p, i + 1);
    }
}

void check(){
    string temp;
    int m = static_cast<int>(log10(n));

    rep(i,s.size() - m){
        string temp = s.substr(i,m);
        State it = s.begin();
        if(number(it) == n){
            range(j,i,i + m){
                f[j] = 1;
            }
        }
    }
}


int main(){
    while(cin >> n >> s, n||s[0] != '0'){
        m.clear();
        ans_num = -1;
        rep(i,8) f[i] = 0;

        solve("", 0);

        State it = s.begin();
        if(ans_num == -1){
            cout << "error" << endl;
        }else if(m[ans_num] > 1){
            cout << "rejected" << endl;
        }else{
            cout << ans_num << ' ';
            rep(i,ans_str.size()){
                if(ans_str[i] == '+') cout << ' ';
                else cout << ans_str[i];
            }
            cout << endl;
        }
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define foreach(i,c) for(auto i=(c).begin();i!=(c).end();++i)
#define all(c) begin(c),end(c)
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

bool valid(vs ns)
{
	return any_of(all(ns),[](string n){return n=="0" || n[0]!='0';});
}
int sum(vs ns)
{
	return accumulate(all(ns),0,[](int x,string s){return x+atoi(s.c_str());});
}

int main()
{
	for(int t,n;cin>>t>>n,t|n;){
		string ds;
		{ ostringstream oss; oss<<n; ds=oss.str(); }
		
		vs res; int cnt=0;
		rep(i,1<<ds.size()-1){
			vs tmp(1); tmp[0]+=ds[0];
			rep(j,ds.size()-1){
				if(i&1<<j)
					tmp.push_back("");
				tmp.back()+=ds[j+1];
			}
			if(valid(tmp) && sum(tmp)<=t){
				if(res.empty() || sum(tmp)>sum(res))
					res=tmp,cnt=1;
				else if(sum(tmp)==sum(res))
					cnt++;
			}
		}
		if(cnt==0)
			cout<<"error"<<endl;
		else if(cnt>1)
			cout<<"rejected"<<endl;
		else{
			cout<<sum(res);
			rep(i,res.size()) cout<<' '<<res[i];
			cout<<endl;
		}
	}
}