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;
 
signed main(){
 
  while(true){
    int target;
    pair<int, pair<int,int> > ans = {-1,{0,0}};
    string num;
     
    cin>>target>>num;
 
    if(target == 0 && num == "0") break;
 
    for(int i = 0; i < (1<<(num.size()-1)); i++){
      int sum = 0, n = 0;
      for(int j = 0; j < num.size(); j++){
    n = n*10 + (num[j] - '0');
    if((i>>j)&1 || j == num.size()-1){
      sum += n;
      n = 0;
    }
      }
      if(sum >  target) continue;
      if(ans.first == sum) ans.second.first++;
      else if(ans.first < sum){
      ans = make_pair(sum,make_pair(1,i));
      }
    }
 
    if(ans.second.first > 1) cout<<"rejected"<<endl;
    else if(ans.first == -1) cout<<"error"<<endl;
    else {
      int n = 0;
      cout<<ans.first;
       
      for(int j = 0; j < num.size(); j++){
    n = n*10 + (num[j] - '0');
    if((ans.second.second>>j)&1 || j == num.size()-1){
      cout<<" "<<n;
      n = 0;
    }
      }
      cout<<endl;
    }
     
  }
   
 
  return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

#define BIG_NUM 2000000000

using namespace std;

bool strCmp(char* base, char* comp){
	int length1 = 0,length2 = 0;
	for(int i=0;base[i] != '\0'; i++)length1++;
	for(int i=0;comp[i] != '\0'; i++)length2++;
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

int main(){

	char left[7],right[7];
	int left_num,right_len,maximum,first_check,limit,work[6],tmp,tmp_sum;
	int maximum_count;
	queue<int> ANS,TMP_ANS;

	while(true){
		scanf("%s %s",left,right);
		if(left[0] == '0' && right[0] == '0')break;

		if(strCmp(left,right)){
			printf("%s %s\n",left,right);
			continue;
		}

		left_num = 0;
		for(int i = 0; left[i] != '\0'; i++){
			left_num = 10*left_num + left[i] - '0';
		}

		first_check = 0;
		for(right_len = 0;right[right_len] != '\0';right_len++){
			first_check += right[right_len] - '0';
		}
		if(first_check > left_num){
			printf("error\n");
			continue;
		}else{
			if(right_len == 1){
				printf("%d %d\n",first_check,first_check);
				continue;
			}
		}

		maximum = -1;
		maximum_count = 0;

		limit = pow(2,right_len-1);

		while(!ANS.empty())ANS.pop();
		while(!TMP_ANS.empty())TMP_ANS.pop();

		for(int i = 0; i < limit; i++){

			for(int k = 0; k < right_len-1; k++){
				if(i & (1 << k)){
					work[k] = 1;
				}else{
					work[k] = 0;
				}
			}

			tmp = right[0] - '0';
			tmp_sum = 0;

			while(!TMP_ANS.empty())TMP_ANS.pop();

			for(int k = 1; k <= right_len-1;k++){
				if(work[k-1] == 0){
					tmp = 10*tmp + right[k] - '0';
					if(k == right_len-1){
						tmp_sum += tmp;
						TMP_ANS.push(tmp);
						if(tmp_sum > maximum && tmp_sum <= left_num){
							while(!ANS.empty())ANS.pop();
							while(!TMP_ANS.empty()){
								ANS.push(TMP_ANS.front());
								TMP_ANS.pop();
							}
							maximum = tmp_sum;
							maximum_count = 1;
						}else if(tmp_sum == maximum && tmp_sum <= left_num){
							maximum_count++;
						}
					}
				}else{
					tmp_sum += tmp;
					TMP_ANS.push(tmp);
					tmp = right[k] - '0';
					if(k == right_len-1){
						tmp_sum += tmp;
						TMP_ANS.push(tmp);
						if(tmp_sum > maximum && tmp_sum <= left_num){
							while(!ANS.empty())ANS.pop();
							while(!TMP_ANS.empty()){
								ANS.push(TMP_ANS.front());
								TMP_ANS.pop();
							}
							maximum = tmp_sum;
							maximum_count = 1;
						}else if(tmp_sum == maximum && tmp_sum <= left_num){
							maximum_count++;
						}
					}
				}
			}
		}

		if(maximum_count == 0){
			printf("error\n");
		}else if(maximum_count > 1){
			printf("rejected\n");
		}else{
			printf("%d",maximum);
			while(!ANS.empty()){
				printf(" %d",ANS.front());
				ANS.pop();
			}
			printf("\n");
		}
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;
int stoi(string x){stringstream ss;ss<<x;int tmp;ss>>tmp;return tmp;}
string itos(int x){stringstream ss;ss<<x;return ss.str();}
#define sz(n)  (int)n.size()
#define dump(x) cerr<<#x<<" = "<<(x)<<endl;
#define debug(x)cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define MP make_pair
#define PB push_back
//	priority_queue<int,vector<int>,greater<int>() >ta;
	int n,m;
	const int BIG=1000000;
	string sn,sm;
	int ans=0;
	bool used[BIG];
	bool flag[100];
	vector<string>ab;
	
	void dfs(int h,int f){
		if(h==sz(sm)){
		//for(int i=0;i<sz(sm);i++)cout<<flag[i]<<" ";cout<<endl;
		int sum=0;
		string tmp="";
		flag[sz(sm)-1]=0;
		for(int i=0;i<sz(sm);i++){
			tmp.push_back(sm[i]);
			if(flag[i])sum+=stoi(tmp),tmp="";
		}
		if(sz(tmp)>=1)sum+=stoi(tmp);
		if(f==1&&sum==ans){
			//for(int i=0;i<sz(sm);i++)cout<<flag[i]<<" ";cout<<endl;
			tmp="";
			for(int i=0;i<sz(sm);i++){
			tmp.push_back(sm[i]);
			if(flag[i]){ab.push_back(tmp),tmp="";}
		}
		if(!tmp.empty())ab.push_back(tmp);
		return ;
		}
		
		
		if(sum<=n)ans=max(ans,sum);
		used[sum]++;
		//for(int i=0;i<sz(sm);i++)cout<<flag[i]<<" ";cout<<sum<<endl;
		return ;
		}
		
		flag[h]=1;
		dfs(h+1,f);
		flag[h]=0;
		if(h+1<sz(sm))dfs(h+1,f);
	}
	
	
	int main(){
	while(cin>>n>>m,n||m){
	sn=itos(n),sm=itos(m);
	fill(used,used+BIG,0);
	fill(flag,flag+100,0);
	ans=0;
	ab.clear();
	dfs(0,0);
	if(used[ans]>=2)cout<<"rejected"<<endl;
	else if (ans==0)cout<<"error"<<endl;
	else {
		cout<<ans<<" ";
		dfs(0,1);
		for(int i=0;i<sz(ab);i++){cout<<ab[i];(i==sz(ab)-1)?cout<<"\n":cout<<" ";}
	}
	}
	return 0;
}