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;
typedef long long ll;
const ll MOD = 1e9+7;
typedef pair<int,int> P;
#define INF 1e9
#define rep(i,N) for(int i=0;i<N;i++)

int main(){
    int t,num;
    while(cin>>t>>num){
        if(t == 0)break;
        int numd = 1 , tmp = num;
        bool isReject = false;
        string nums = to_string(num);
        while(tmp/=10){
            numd++;
        }
        //cout<<numd<<endl;
        int loop = pow(2,numd-1);
        int ans = 0,dist = 1e6;
        vector<int>ansm;
        rep(i,loop){
            int res = 0;
            int sum = 0;
            int b = 0;
            vector<int>m;
    		// for (int j=0; j<numd-1; j++) {
			// 	if ((1&i>>j) == 1) {
            //         if(j>0)b += j*2;
            //         else if(j==0)b += 1;
            //     }
			// }
            //cout<<"i:"<<i<<" b:"<<b<<endl;
            bool flag = true;
            for(int j = 0; j<numd; j++){
                sum *= 10;
                //cout<<numd-j<<endl;
                sum += (nums[j] - '0');
                if((1&i>>j) == 1){
                    //cout<<"sum:"<<sum<<endl;
                    res += sum;
                    m.push_back(sum);
                   // cout<<"sum:"<<sum<<" res:"<<res<<endl;
                    //cout<<"res:"<<res<<endl;
                    sum = 0;
                    flag = true;
                }else{
                    flag = false;
                }
            }
            if(!flag){
                res += sum;
               // cout<<"sum:"<<sum<<" res:"<<res<<endl;
                m.push_back(sum);
                //res += (nums[0] - '0');
            }

            //cout<<res<<endl;
            // if(t-res == 1){
            //     for(auto itr : ansm){
            //         cout<<itr<<" ";
            //     }
            //     cout<<res<<endl;
            // }
            if(res > t)continue;
            if(dist > (t-res)){
                dist = t-res;
                ansm = m;
                isReject = false;
            }else if(dist == (t-res)){
                isReject = true;
            }
//            dist = min(dist , t - res);
        }
        if(isReject){
            cout<<"rejected"<<endl;
        }else if(ansm.empty()){
            cout<<"error"<<endl;
        }else{
            ans = t - dist;
            cout<<ans;
            for(auto itr:ansm){
                cout<<" "<<itr;
            }
            cout<<endl;
        }
    }
}

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;
}

C++: #include<iostream>
#include<cmath>
using namespace std;
int target;

int main(int argc, char *argv[]){
  while(cin >> target, target){
    int card;
    cin >> card;
    int ans = -1, digit = log10(card), bit = 0;

    for (int i = 0; i < 1<<digit; i++) {
      int tmp = card, calc = 0;
      for (int j = digit - 1; j >= 0; j--) {
        if(i&(1<<j)){
          calc += tmp/(int)pow(10, j+1);
          tmp %= (int)pow(10, j+1);
        }
      }
      calc += tmp;
      if(ans < calc && calc <= target){
        // std::cout << "calc:" << calc << std::endl;
        ans = calc;
        bit = i;
      }else if(ans == calc){
        bit = -1;
      }
    }
    if(ans == -1){
      std::cout << "error" << std::endl;
    }else if (bit == -1){
      std::cout << "rejected" << std::endl;
    }else{
      //std::cout << "bit:" << bit << std::endl;
      std::cout << ans << " ";
      for (int i = digit - 1; i >= 0; i--) {
        std::cout << (int)(card/pow(10, i+1));
        card %= (int)pow(10, i+1);
        if(bit&(1<<i))std::cout << " ";
      }
      std::cout << card << std::endl;
    }
  }
  return 0;
}