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 <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
using namespace std;
typedef pair<int, vector<string> > P;

int t;
string s;

P f(string s, vector<bool> cut) {
	P ret;
	string a;
	a += s[0];
	for(int i = 0; i < cut.size(); i++) {
		if(cut[i]) {
			ret.second.push_back(a);
			a = s[i + 1];
		}
		else {
			a += s[i + 1];
		}
	}
	ret.second.push_back(a);
	for(int i = 0; i < ret.second.size(); i++) {
		if(ret.second[i].size() != 1 && ret.second[i][0] == '0') {
			ret.first = t + 1;
			break;
		}
		else {
			ret.first += atoi(ret.second[i].c_str());
		}
	}
	return ret;
}

vector<vector<string> > ans[1000000];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	while(cin >> t >> s, t && s != "0") {
		for(int i = 0; i < 1000000; i++) {
			ans[i].clear();
		}
		int n = s.size();
		int m = -1;
		for(int bit = 0; bit < 1 << (n - 1); bit++) {
			vector<bool> cut(n - 1);
			for(int i = 0; i < n - 1; i++) {
				if(bit >> i & 1) cut[i] = true;
			}
			P p = f(s, cut);
			if(p.first > t) continue;
			m = max(m, p.first);
			ans[p.first].push_back(p.second);
		}
		if(m == -1) {
			cout << "error" << endl;
		}
		else if(ans[m].size() >= 2) {
			cout << "rejected" << endl;
		}
		else {
			cout << m;
			for(string v : ans[m][0]) {
				cout << " " << v;
			}
			cout << endl;
		}
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <ctime>
#include <list>
#include <numeric>
#include <fstream>

using namespace std;
static const double EPS = 1e-8;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> PI;
#ifndef M_PI
const double M_PI=acos(-1.0);
#endif
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FORR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int((a).size()))
#define F first
#define S second
void pkuassert(bool b){if(!b){int a=0;cout << 1/a << endl;}};
int dx[]={0,-1,0,1,1,1,-1,-1},dy[]={1,0,-1,0,1,-1,1,-1};

string in;

int num(int s,int t){
  int ret=0;
  rep(i,t-s) ret=ret*10+in[s+i]-'0';
  return ret;
}
int memo[6][1000000];

int rec(int ne,int les,bool pr=false){
  if(les<0) return 0;
  if(ne==SZ(in)) return les==0;
  if(!pr && memo[ne][les]!=-1) return memo[ne][les];
  if(num(ne,SZ(in))<les) return 0;
  int& ret=memo[ne][les]=0;
  
  for(int i=ne+1;i<=SZ(in);++i){
    ret += rec(i,les-num(ne,i));
    if(pr && ret){
      cout << ' ' << in.substr(ne,i-ne);
      rec(i,les-num(ne,i),true);
      return 0;
    }
    if(ret>1) return ret=2;
  }
  return ret;
}

int t;
void solve(){
  memset(memo,-1,sizeof(memo));
  while(t){
    if(rec(0,t)==1){
      cout << t;
      rec(0,t,true);
      cout << endl;
      return;
    }
    if(rec(0,t)>1){
      cout << "rejected" << endl;
      return;
    }
    --t;
  }
  cout << "error" << endl;
}

int main(){
  while(cin >> t >> in && t && in!="0") solve();
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int target;
int ans,anscnt;
vector <int> ans2;
vector <int> n;
int flg[7][1000001];
void saiki(int ima,int sum){
  int t = 0;
  if(ima == n.size()){
    if(sum <= target && ans < sum) ans = sum,anscnt = 1,flg[ima][sum] = 1,ans2.clear();
  else if(ans == sum) anscnt++;
  }

  if(sum<=target){
    for(int i=ima;i<n.size();i++){
      t*=10;
      t+=n[i];
      saiki(i+1,sum+t);
      if(flg[i+1][sum+t] ==1) flg[ima][sum] = 1,ans2.push_back(t);
      // else if(flg[ima][sum] == 1) anscnt++;
    }
  }
}



int main() {

  while(1) {
    int a;
    cin >> target >> a;
    if(target == 0 && a == 0 ) break;
    
    int c = 0;
    n.clear();
    while(a != 0){
      n.push_back(a%10);
      a /= 10;
    }
    reverse(n.begin(),n.end());
    ans = -1;
    anscnt = 0;
    saiki(0,0);
    if(anscnt >=2) cout << "rejected"<<endl;
    else if(ans == -1) cout << "error"<<endl;
    else {
      cout << ans <<" ";
      for(int i=0;i<ans2.size();i++)cout << ans2[ans2.size()-1-i]<<" ";
      cout << endl;
    }
  }

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

vector<int> calc(int t, string &num, int key){
	vector<int> sum;
	string::reverse_iterator it = num.rbegin();
	int temp = *it - '0';
	int mlt = 10;
	it++;
	for(int i = 1; i <= 1 << (num.size() -2); i <<= 1){
		if((key & i) == i){
			sum.push_back(temp);
			temp = *it - '0';
			mlt = 10;
		}
		else{
			temp += (*it - '0')*mlt;
			mlt *= 10;
		}
		it++;
	}
	sum.push_back(temp);
	/*
	for(int i = 0; i < sum.size(); i++){
		cout << sum[i] << " ";
	}
	cout << endl;
	*/
	return sum;
}

int main(){
	while(true){
		int t;
		string num;
		cin >> t >> num;
		if(t == 0 )break;
		vector<int> ans(1);
		ans[0] = atoi(num.c_str());
		int anssum = t >= ans[0] ? ans[0] : 0;
		bool flag = false;
		for(int i = 1; i < (1 << (num.size()-1)); i++){
			vector<int> temp = calc(t,num,i);
			int sum = accumulate(temp.begin(),temp.end(),0);
			if(sum > t)continue;
			if(sum > anssum){
				anssum = sum;
				swap(ans,temp);
				flag = false;
			}
			else if(sum == anssum){
				flag = true;
			}
		}
		if(anssum == 0){
			cout << "error" << endl;
		}
		else if(flag){
			cout << "rejected" << endl;
		}
		else {
			cout << anssum << " ";
			for(int i = ans.size()-1; i >= 0; i--){
				cout << ans[i];
				if(i != 0){
					cout << " ";
				}
			}
			cout << endl;
		}
	}
		
	return 0;
}