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;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll (i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for(ll (i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for(ll (i) = (n) - 1; (i) >= 0; (i)--)
#define rrep1(i, n) for(ll (i) = (n); (i) >= 1; (i)--)
const ll INF = 1145141919;
const ll MOD = 1000000007;
template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}

ll t;
string n;

ll ans1, ans2;
vector<string>ans_v;

void solve(vector<string> v){
	// for(ll i = 0; i < v.size(); i++)cout << v[i] << ",";
	// cout << endl;
	ll sum = 0;
	for(ll i = 0; i < v.size(); i++){
		stringstream ss;
		ss << v[i];
		ll tmp;
		ss >> tmp;
		sum += tmp;
	}
	if(t < sum)return;
	if(ans1 == sum)ans2++;
	if(ans1 < sum){
		ans1 = sum;
		ans2 = 1;
		ans_v = v;
	}
}

int main(){

	for(;cin >> t >> n;){
		if(t == 0 && n == "0")break;
		ans1 = -1;
		ans2 = 0;
		for(ll i = 0; i < (1 << (n.size() - 1)); i++){
			vector<string>v;
			string tmp = "";
			for(ll j = 0; j < n.size(); j++){
				tmp += n[j];
				if(i & (1 << j)){
					v.push_back(tmp);
					tmp = "";
				}
			}
			if(tmp != "")v.push_back(tmp);
			solve(v);
		}
		if(ans1 < 0)cout << "error" << endl;
		else if(ans2 > 1)cout << "rejected" << endl;
		else{
			cout << ans1;
			rep(i, ans_v.size())if(ans_v[i].size())cout << " " << ans_v[i];
			cout << endl;
		}
	}

    return 0;
}

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 << "errot"<<endl;
    else {
      cout << ans <<" ";
      for(int i=0;i<ans2.size();i++)cout << ans2[ans2.size()-1-i]<<" ";
      cout << endl;
    }
  }

    return 0;
}

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++) {
		a = ret.second[i];
		while(a.size() > 1 && a.front() == '0') a = a.substr(1, a.size() - 1);
		ret.first += atoi(a.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 <stdio.h>
#pragma warning(disable : 4996)
int n, m, d, ret, a[6], b[6], c[6], e, f, g;
int digit(int x) {
	if (x < 10) return 1;
	return digit(x / 10) + 1;
}
int power(int a, int b) {
	if (b == 0) return 1;
	return power(a, b - 1) * a;
}
int main() {
	while (true) {
		scanf("%d%d", &n, &m);
		if (n == 0 && m == 0) break;
		d = digit(m); g = 0;
		for (int i = 0; i < d; i++) {
			a[i] = (m / power(10, d - i - 1) ) % 10;
		}
		ret = 0;
		for (int i = 0; i < (1 << (d - 1)); i++) {
			e = 0; f = a[0];
			for (int j = 1; j < d; j++) {
				if (i & (1 << (j - 1))) {
					c[e++] = f; f = a[j];
				}
				else {
					f = f * 10 + a[j];
				}
			}
			c[e++] = f;
			int sum = 0;
			for (int j = 0; j < e; j++) {
				sum += c[j];
			}
			if (g <= sum && sum <= n) {
				if (g < sum) ret = 1;
				else ret++;
				g = sum;
				for (int j = 0; j < 6; j++) {
					if (j >= e) {
						b[j] = -1;
					}
					else {
						b[j] = c[j];
					}
				}
			}
		}
		if (ret == 0) {
			printf("error\n");
		}
		else if (ret >= 2) {
			printf("rejected\n");
		}
		else {
			printf("%d", g);
			for (int i = 0; i < 6; i++) {
				if (b[i] != -1) {
					printf(" %d", b[i]);
				}
			}
			printf("\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

vector<int> res;
int r;
bool rej;

void func(vector<int> vec, string str, int t, int s)
{
	if(str.size() == 0){
		if(s > t){
			return ;
		} else if(r == s){
			rej = true;
		} else if(r < s){
			rej = false;
			r = s;
			res = vec;
		}
	} else {
		string s1 = "", s2;
		for(int i = 0; i < str.size(); i++){
			s1 += str[i];
			if(i+1 < str.size()){
				s2 = str.substr(i+1);
			} else {
				s2 = "";
			}
			int v = atoi(s1.c_str());
			vector<int> v2 = vec;
			v2.push_back(v);
			func(v2, s2, t, s+v);
		}
	}
	
	return ;
}

int main()
{
	int t;
	string str;
	
	while(cin>>t>>str && !(t == 0 && str == "0")){
		vector<int> vec;
		rej = false;
		r = -1;
		func(vec, str, t, 0);
		if(res.size() == 0){
			cout << "error" << endl;
		} else {
			if(rej){
				cout << "rejected" << endl;
			} else {
				cout << r;
				for(int i = 0; i < res.size(); i++){
					cout << " " << res[i];
				}
				cout << endl;
			}
		}
		
		res.clear();
	}
	
	return 0;
}