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 <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
#include <string>
#include <sstream>
#include <bitset>
using namespace std;
#define INF	100000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define rep(i,n) for(int i = 0; i < n; i++)
#define SORT(x) sort((x).begin(), (x).end())
#define clr(a,b) memset((a),(b),sizeof(a))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<P> Vp;
typedef priority_queue<P, vector<P>, greater<P> > PQ;

bool check(int a, int b, Vi v){
	int sum = 0;
	rep(i,a) sum += v[i];
	if(sum > b) return true;
	else return false;
}

int ipow(int a){
	int ret = 1;
	rep(i,a) ret *= 10;
	return ret;
}

int M(Vi v, int a){
	int ret = 0;
	rep(i,a) ret += v[i]*ipow(a-1-i);
	return ret;
}

int main(){
	int n;
	string str;
	while(cin >> n >> str){
		if(n == 0 && str[0] == '0') break;
		int p = str.size();
		Vi num(p);
		rep(i,p) num[i] = str[i]-'0';
		int sum = -1;
		Vi ans;
		if(check(p,n,num)){
			cout << "error" << endl;
			continue;
		}
		if(n >= M(num,p)){
			cout << M(num,p) << " " << M(num,p) << endl;
			continue;
		}
		
		for(int i = 0; i < p-1; i++){
			//if(num[i+1] == 0 && i != p-1) continue;
			int x[2];
			rep(f,2) x[f] = 0;
			for(int f = 0; f <= i; f++) x[0] += num[f]*ipow(i-f);
			for(int f = i+1; f < p; f++) x[1] += num[f]*ipow(p-f-1);
			int tmp = 0;
			rep(f,2) tmp += x[f];
			if(tmp == sum) ans.clear();
			else if(tmp > sum && tmp <= n){
				ans.clear();
				sum = tmp;
				rep(f,2) ans.pb(x[f]);
			}
		}
		for(int i = 0; i < p-2; i++){
			if(num[i+1] == 0) continue;
			for(int j = i+1; j < p-1; j++){
				//if(num[i+1] == 0 && j != i+1) break;
				//if(num[j+1] == 0 && j != p-2) continue;
				int x[3];
				rep(f,3) x[f] = 0;
				for(int f = 0; f <= i; f++) x[0] += num[f]*ipow(i-f);
				for(int f = i+1; f <= j; f++) x[1] += num[f]*ipow(j-f);
				for(int f = j+1; f < p; f++) x[2] += num[f]*ipow(p-f-1);
				int tmp = 0;
				rep(f,3) tmp += x[f];
				if(tmp == sum) ans.clear();
				else if(tmp > sum && tmp <= n){
					ans.clear();
					sum = tmp;
					rep(f,3) ans.pb(x[f]);
				}
			}
		}
		for(int i = 0; i < p-3; i++){
			for(int j = i+1; j < p-2; j++){
				//if(num[i+1] == 0 && j != i+1) break;;
				for(int k = j+1; k < p-1; k++){
					//if(num[j+1] == 0 && k != j+1) break;
					//if(num[k+1] == 0 && k != p-2) continue;
					int x[4];
					rep(f,4) x[f] = 0;
					for(int f = 0; f <= i; f++) x[0] += num[f]*ipow(i-f);
					for(int f = i+1; f <= j; f++) x[1] += num[f]*ipow(j-f);
					for(int f = j+1; f <= k; f++) x[2] += num[f]*ipow(k-f);
					for(int f = k+1; f < p; f++) x[3] += num[f]*ipow(p-f-1);
					int tmp = 0;
					rep(f,4) tmp += x[f];
					if(tmp == sum) ans.clear();
					else if(tmp > sum && tmp <= n){
						ans.clear();
						sum = tmp;
						rep(f,4) ans.pb(x[f]);
					}
				}
			}
		}
		for(int i = 0; i < p-3; i++){
			for(int j = i+1; j < p-2; j++){
				//if(num[i+1] == 0 && j != i+1) break;;
				for(int k = j+1; k < p-1; k++){
					//if(num[j+1] == 0 && k != j+1) break;
					for(int l = k+1; l < p-1; l++){
						//if(num[k+1] == 0 && l != k+1) break;
						//if(num[l+1] == 0 && l!= p-2) continue;
						int x[5];
						rep(f,5) x[f] = 0;
						for(int f = 0; f <= i; f++) x[0] += num[f]*ipow(i-f);
						for(int f = i+1; f <= j; f++) x[1] += num[f]*ipow(j-f);
						for(int f = j+1; f <= k; f++) x[2] += num[f]*ipow(k-f);
						for(int f = k+1; f <= l; f++) x[3] += num[f]*ipow(l-f);
						for(int f = l+1; f < p; f++) x[4] += num[f]*ipow(p-f-1);
						int tmp = 0;
						rep(f,5) tmp += x[f];
						if(tmp == sum) ans.clear();
						else if(tmp > sum && tmp <= n){
							ans.clear();
							sum = tmp;
							rep(f,5) ans.pb(x[f]);
						}
					}
				}
			}
		}
		if(ans.size() == 0){
			cout << "rejected" << endl;
		} else{
			cout << sum;
			rep(i,ans.size()) cout << " " << ans[i];
			cout << endl;
		}
	}
}

C++: #include<iostream>
#include<string>
#include<stack>
#include<list>
#include<cstdio>

using namespace std;

class Ans{
  int minimam;
  int target;
  list<int> st;
  stack<string> ans;
  const char *str;
  void prl(){
    for(list<int>::iterator it=st.begin();it!=st.end();it++){
      printf("%d ",*it);
    }
    printf("\n");
  }

  void check(int n){
    if(n<0){
      return;
    }else if(n<=minimam){
      char buff[10];
      string s;
      sprintf(buff,"%d ",target-n);
      s=buff;
      for(list<int>::iterator it=st.begin();it!=st.end();++it){
        sprintf(buff,"%d",*it);
        s+=buff;
      }
      if(n<minimam){
        ans=stack<string>();
      }
      while(s[s.size()-1] == ' ') s = s.substr(0, s.size()-1);
      ans.push(s);
      minimam=n;
    }
  }
  void answer(const char *str,int now,int dist){
    if(*str == '\0' && now!=-1) {
      st.push_back(now);
      check(dist-now);
      st.pop_back();
      return;
    }
    if(now!=-1){
      st.push_back(now);
      //cout << now << endl;
      //prl();
      answer(str,-1,dist-now);
      st.pop_back();
    }
    {
      if(now==-1) now=0;
      now=now*10+(*str - '0');
      if(now > dist) return;
      answer(str+1,now,dist);
    }
  }
public:
  Ans(int target,const char *str){
    this->target = target;
    this->str = str;
    this->minimam = 0x7FFFFFFF;
    answer(this->str,-1,this->target);
  }
  void print(){
    if(ans.size()==1){
      cout << ans.top() << endl;
    }else if(ans.size()>1){
      cout << "rejected" << endl;
    }else{
      cout << "error" << endl;
    }
  }
};

int main(){
  int n;
  string m;
  while(cin >> n >> m , !(n==0&&m=="0")){
    Ans a(n,m.c_str());
    //cout << endl;
    a.print();
    //cout << endl;
  }
  return 0;
}