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<vector>
#include<map>
using namespace std;

vector<string> con, res;
int score, t;
string s;
map<int,int> m;

void dfs(int cur){
    if(cur == s.length()){
        int tmp = 0;
        for(string x : con) tmp += stoi(x);
        if(tmp <= t && tmp >= score){
            score = tmp;
            res = con;
            m[tmp]++;
        }
        return;
    }

    for(int k = cur+1; k <= s.length(); k++){
        con.push_back(s.substr(cur, k-cur));
        dfs(k);
        con.pop_back();
    }
}

int main(){
    while(cin >> t >> s, t != 0 && s != "0"){
        m.clear();
        con.clear();
        score = -1;
        int tmp = 0;
        for(char c : s)   tmp += c-'0';
        if(t < tmp){
            cout << "error" << endl;
            continue;
        }
        dfs(0);
        if(m[score] != 1){
            cout << "rejected" << endl;
        }else{
            cout << score << " ";
            for(int i = 0; i < res.size(); i++)    cout << res[i] << " \n"[i+1==res.size()];
        } 
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
using namespace std;
#define INFTY (1<<21)
int target;
string source;
vector<vector<string> > ans;
int diff;

void update(vector<int> c ){
    vector<string> s;
    for ( int i = 0; i < c.size()-1; i++ ){
	s.push_back(source.substr(c[i], c[i+1]-c[i]));
    }
    int sum = 0;
    for ( int i = 0; i < s.size(); i++ ) sum += atoi(s[i].c_str());

    if ( sum > target ) return;

    if ( target - sum == diff ){
	ans.push_back(s);
    } else if ( target - sum < diff ){
	diff = target - sum;
	ans.clear();
	ans.push_back(s);
    }
}

void rec(int pos, vector<int> c){
    if ( pos == source.size() ){
	c.push_back(pos);
	update(c);
	return;
    }
    rec(pos+1, c);
    c.push_back(pos);
    rec(pos+1, c);
    c.pop_back();
}

void compute(){
    vector<int> c;
    c.push_back(0);
    diff = INFTY;
    ans.clear();
    rec(1, c);

    if ( diff == INFTY ) cout << "error" << endl;
    else if ( ans.size() > 1 ) cout << "rejected" << endl;    
    else { 
	cout << target - diff;
	for ( int i = 0; i < ans[0].size(); i++ ) cout << " " << ans[0][i];
	cout << endl;
    }
}

main(){
    while( cin >> target >> source){
	if ( target == 0 && source == "0" ) break;
	compute();
    }
}

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 << "reject" << 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;

		}

	}
}

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

int Max,x;
string s;
bool ck;
vector<string> ans;

void dfs(int k, int sum, string p, vector<string> a) {
  if(sum>x) return;
  if(k==s.size()) {
    int z;
    stringstream ss;
    ss << p;
    ss >> z;
    sum+=z;
    a.push_back(p);
    if(sum>x) return;
    if(sum==Max) ck=false;
    else if(sum>Max) {
      Max=sum;
      ans=a;
      ck=true;
    }
    return;
  }
  dfs(k+1,sum,p+s[k],a);
  a.push_back(p);
  int z;
  stringstream ss;
  ss << p;
  ss >> z;
  string t="";
  t+=s[k];
  dfs(k+1,sum+z,t,a);
}

int main() {
  int d;
  while(cin >> x >> d) {
    if(x==0 && d==0) break;
    ck=false;
    Max=-1;
    ans.clear();
    stringstream ss;
    ss << d;
    ss >> s;
    dfs(0,0,"",ans);
    if(ck) {
      cout << Max;
      for(int i=0; i<ans.size(); i++) cout << " " << ans[i];
      cout << endl;
    } else {
      if(Max==-1) cout << "error" << endl;
      else cout << "rejected" << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int t, n;
  while (cin >> t >> n, t) {
    int m = to_string(n).size();
    V<> a;
    map< int, VV<> > mp;
    auto dfs = [&](auto dfs) -> void {
      if (accumulate(begin(a), end(a), 0) == m) {
        int i = 0, s = 0;
        for (int e : a) {
          s += stoi(to_string(n).substr(i, e));
          i += e;
        }
        if (s <= t) mp[s].push_back(a);
        return;
      }
      for (int i = 1; accumulate(begin(a), end(a), 0) + i <= m; ++i) {
        a.push_back(i);
        dfs(dfs);
        a.pop_back();
      }
    };
    dfs(dfs);
    if (mp.empty()) cout << "error\n";
    else if (rbegin(mp)->second.size() >= 2) cout << "rejected\n";
    else {
      cout << rbegin(mp)->first;
      int i = 0;
      for (int e : rbegin(mp)->second[0]) {
        cout << ' ' << to_string(n).substr(i, e);
        i += e;
      }
      cout << '\n';
    }
  }
}

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

int main()
{
	int t;
	string num;
	while(1)
	{
		cin>>t>>num;
		if(t==0 && num=="0")
			break;
		if(to_string(t)==num)
		{
			cout<<num<<" "<<num<<endl;
			continue;
		}
		int keta=num.size()-1;//仕切りの数を求める
		//cerr<<keta<<" "<<(1<<keta)<<endl;
		int sum[1<<6];//それぞれの和
		fill(sum,sum+(1<<6),1e8-1);
		int m=0;
		int mind=-1;
		for(int bit=0;bit<(1<<keta);bit++)
		{
			//bitが1のときnumの以下に対応するところで切る
			//num 1 2 3 4 5 6
			//bit  4 3 2 1 0
			sum[bit]=0;
			int tmp=0;
			int cnt=1;
			int last=-1;
			for(int i=0;i<keta+1;i++)
			{
				tmp+=(num[i]-'0');
				if(bit&(1<<i))
				{
					sum[bit]+=tmp;
					tmp=0;
					cnt=0;
					last=i;
				}
				tmp*=10;
				cnt++;
			}
			//cerr<<bitset<4>(bit)<<" "<<last<<" "<<cnt<<endl;
			sum[bit]+=(tmp/10);
			if(sum[bit]>t)
				sum[bit]=1e8-1;
			if(sum[bit]!=1e8-1 && sum[bit]>m)
			{
				m=sum[bit];
				mind=bit;
			}
		}
		//cerr<<mind<<" "<<m<<endl;
		if(mind==-1)
		{
			cout<<"error"<<endl;
			continue;
		}
		//重複がないか確かめる
		bool f=false;
		for(int i=0;i<(1<<6);i++)
		{
			if(sum[i]==m && mind!=i)
			{
				cout<<"rejected"<<endl;
				f=true;
				break;
			}
		}
		if(f)
			continue;
		//構築
		cout<<m<<" ";
		for(int i=0;i<keta+1;i++)
		{
			cout<<num[i];
			if(mind&(1<<i))
				cout<<" ";
		}
		cout<<endl;
	}
}