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>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

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

using namespace std;
int main(void)
{
  for(;;){
    int t;
    string num;
    cin >> t >> num;
    if(t==0&&num=="0") return 0;
    vector<pair<int,vector<int> > >answers;
    REP(i,1<<(num.size()-1)){
      vector<int> sep;
      sep.push_back(0);
      REP(j,num.size()-1) if(1&(i>>j)) sep.push_back(j+1);
      sep.push_back(num.size());
      int tmpans=0;
      vector<int> vi;
      REP(j,sep.size()-1){
	int tmp=stoi(string(num.begin()+sep[j],num.begin()+sep[j+1]));
	tmpans+=tmp;
	vi.push_back(tmp);
      }
      if(tmpans<=t) answers.push_back(make_pair(tmpans,vi));
    }
    SORT(answers);
    if(answers.empty()) cout << "error" << endl;
    else{
      int as=answers.size();
      if(as==1||answers[as-2].first!=answers[as-1].first){
	cout << answers[as-1].first;
	for(int p:answers[as-1].second) cout << ' ' << p;
	cout << endl;
      }else{
	cout << "rejected" << endl;
      }
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF (1<<28)

int n,len;
string str;
vector<int> t,u,ans;
int mini,cnt;

int calc_sum(vector<int> &vec){
  int res=0;
  for(int i=0;i<(int)vec.size();i++)res+=vec[i];
  return res;
}

int change(int p){
  return str[p]-'0';
}

void rec(int p,int num){
  num=num*10+change(p);
  
  if(p+1==len){
    t.push_back(num);
    int sum=calc_sum(t);
    /*    
    for(int i=0;i<(int)t.size();i++){
      cout<<t[i]<<' ';
    }cout<<endl;
    cout<<sum<<endl;
    */
    if(sum<=n){
      if(n-sum<mini){
	mini=n-sum;cnt=1;ans=t;
      }else if(n-sum==mini)cnt++;
    }
    t.pop_back();
    return;
  }

  rec(p+1,num);
  
  t.push_back(num);
  rec(p+1,0);
  t.pop_back();
  
}

int main(){
  while(1){
    cin>>n>>str;
    if(n==0&&str=="0")break;
    len=str.size();
    mini=INF;
    rec(0,0);
    if(mini==INF){
      cout<<"error"<<endl;
    }else if(cnt==1){
      cout<<n-mini;
      for(int i=0;i<(int)ans.size();i++)
	cout<<' '<<ans[i];
      cout<<endl;
    }else{
      cout<<"rejected"<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int main(){
	string a;
	string s;
	while(cin>>a>>s,a!="0"||s!="0"){
		int A=atoi(&a[0]);
		int m=a.size();
		int out=-1;
		int n=s.size();
		int N=1<<(n-1);
		vi nu;
		bool H=false;
		rep(i,N){
			vi tmp;
			int t=i;
			int sum=0;
			int r=0;
			int co=0;
			bool h=true;
			while(t){
				if(co==m)h=false;
				co++;
				if(t%2){
					int q=atoi(&(s.substr(r,co))[0]);
					sum+=q;
					tmp.pb(q);
					r+=co;
					co=0;
				}
				t/=2;
			}
			int q=atoi(&s.substr(r)[0]);
			sum+=q;
			tmp.pb(q);
			if(A<sum)continue;
			if(h&&out==sum)H=true;
			else if(h&&out<sum){
				H=false;
				nu=tmp;
			}
			out=max(out,sum);
		}
		if(H)cout<<"rejected"<<endl;
		else if(out==-1)cout<<"error"<<endl;
		else{
			cout<<out;
			rep(i,nu.size())cout<<" "<<nu[i];
			cout<<endl;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
	string str;
	int target;
	while(cin >> target >> str, str != "0" || target != 0){
		int len = str.size() - 1;
		int ans = -1;
		int ansi = 0;
		bool reject = false;
		for(int i = 0; i < (1 << len); i++){
			int prev = 0;
			int sum = 0;
			for(int j = 0; j < len; j++){
				if(i & (1 << j)){
					sum += stoi(str.substr(prev, (j - prev)+ 1), nullptr, 10);
					prev = j + 1;
				}
			}
			sum += stoi(str.substr(prev, str.size() - prev),nullptr, 10);
			//cout << sum << endl;
			if(sum <= target){
				if(ans == sum){
					reject = true;	
				}else if(ans < sum){
					ans = sum;
					reject = false;
					ansi = i;
				}
			}
		}
		if(ans == -1){
			cout << "error" << endl;
		}else if(reject){
			cout << "rejected" << endl;
		}else{
			cout << ans;
			int prev = 0;
			for(int j = 0; j < len; j++){
				if(ansi & (1 << j)){
					cout << " " << stoi(str.substr(prev, (j - prev)+ 1),nullptr, 10);
					prev = j + 1;
				}
			}
			cout << " " << stoi(str.substr(prev, str.size() - prev),nullptr, 10);
			cout << endl;

		}

	}
}