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<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=2000000;
	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 "bits/stdc++.h"
#pragma warning(disable:4996)
using namespace std;

map<int, pair<vector<int>,int>>getans(string st) {
	map<int, pair<vector<int>,int>>ans;
	if (st[0] == '0')return ans;
	ans[stoi(st)] = make_pair(vector<int>(1,stoi(st)),1);
	if (st.size()) {
		for (int i = 1; i < st.size(); ++i) {
			map<int, pair<vector<int>, int>>mp(getans(st.substr(i, st.size() - i)));
			for (auto& j : mp) {
				string from = st.substr(0, i);
				vector<int>now(1, stoi(from) );
				if (!ans.count(stoi(from) + j.first)) {		
					now.insert(now.end(), j.second.first.begin(), j.second.first.end());
					ans[stoi(from) + j.first] = make_pair(now,1 );
				}
				else {
					ans[stoi(from) + j.first].second++;
				}
			}
		}
	}
	
	return ans;
}

int main() {
	while (1) {
		int sum; string num; cin >> sum >> num;
		if (sum == 0)break;
		map<int, pair<vector<int>, int>>mp(getans(num));
		auto it(mp.upper_bound(sum));

		if (it == mp.begin()) {
			cout << "error" << endl;
		}
		else if (prev(it)->second.second != 1) {
			cout << "rejected" << endl;
		}
		else {
			cout << prev(it)->first;
			for (int i = 0; i < prev(it)->second.first.size(); ++i) {
				cout <<" "<< prev(it)->second.first[i];
			}
			cout << endl;
		}

	}
	return 0;
}

C++: #include<stdio.h>
#include<vector>
#include<map>
typedef std::vector<int>V;
typedef std::pair<int,V>P;
V s;
std::vector<P>v;
int t,a[]={10,100,1000,10000,100000,1000000};
void F(int n,int d,int c)
{
	if(d-1==c)
	{
		s.push_back(n);
		int m,i;
		for(m=i=0;i<s.size();++i)m+=s[i];
		if(t>=m)
		{if(v[0].first<m)v.clear(),v.push_back(P(m,s));
			else if(v[0].first==m)v.push_back(P(m,s));
		}
		s.pop_back();
	}
	else
	{
		F(n,d,c+1);
		s.push_back(n%a[c]);
		n/=a[c];
		F(n,d-c-1,0);
		s.pop_back();
	}
}
int main()
{
	int a,d,i;
	while(scanf("%d%d",&t,&a),t)
	{
		v.clear();
		v.push_back(P());
		for(i=a,d=0;i;++d)i/=10;
		F(a,d,0);
		if(v[0].first==0)puts("error");
		else if(v.size()>1)puts("rejected");
		else
		{for(printf("%d",v[0].first), i=v[0].second.size()-1;i>=0;--i)
				printf(" %d",v[0].second[i]);
			puts("");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
using namespace std;

#define INF (1<<29)

int toInt(string s){
  istringstream iss(s);
  int a;
  iss >> a;
  return a;
}

int main(){
  int n;
  string s;
  while(cin >> n >> s){
    if(n == 0 && s == "0") break;
    int ans = -1;
    vector<string> ans2;
    map<int, int> is_found;
    
    for(int i = 0 ; i < (1<<s.size()) ; i++){
      vector<string> tmp;
      int pos = 0;
      int point = 0;
      for(int j = 0 ; j < s.size() ; j++){
	if(j == s.size()-1){
	  string t = s.substr(pos, j-pos+1);
	  tmp.push_back(t);
	  pos = j+1;
	  point += toInt(t);
	}
	
	else if((1<<j) & i){
	  string t = s.substr(pos, j-pos+1);
	  tmp.push_back(t);
	  pos = j+1;
	  point += toInt(t);
	}
      }
      
      is_found[point]++;
      if(point <= n && n-point < n-ans){
	ans = point;
	ans2 = tmp;
      }
      /*
      cout << point << ' ';
      for(int b = 0 ; b < tmp.size() ; b++){
	cout << tmp[b] << ' ';
      }
      cout << endl;
      */
    }
    
    //cout << is_found[ans] << endl;
    bool f = true;
    if(is_found[ans] > 2) f = false;
    
    if(ans == -1) cout << "error" << endl;
    else if(!f){
      cout << "rejected" << endl;
    }
    else{
      cout << ans << ' ';
      for(int i = 0 ; i < ans2.size() ; i++){
	cout << ans2[i];
	if(i != ans2.size()-1) cout << ' ';
	else cout << endl;
      }
    }
  }
  return 0;
}