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 <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n)  { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x)         { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};
vector<pair<int, string>> vpis;
int n;
string m;
void dfs(int sum, string sep1, string sep2) {
	if (sum + atoi(sep2.c_str()) <= n){
		if (sep1 != "")
			vpis.push_back({ sum + atoi(sep2.c_str()), sep1 + " " + sep2 });
		else
			vpis.push_back({ sum + atoi(sep2.c_str()), sep2 });
	}

	FOR(i, 1, sep2.length()) {
		int num = sum;
		string ss1 = sep2.substr(0, i);
		string ss2 = sep2.substr(i, sep2.length() - i);
		if (ss1!="")
		num += atoi(ss1.c_str());
		if (num + atoi(ss2.c_str()) <= n){
			if (sep1 != ""){
				vpis.push_back({ num + atoi(ss2.c_str()), sep1 + " " + sep2.substr(0, i) + " " + ss2 });
				while (ss2.length()>1&&ss2[0] == '0'){
					ss2 = ss2.substr(1, ss2.length() - 1);
					vpis.push_back({ num + atoi(ss2.c_str()), sep1 + " " + sep2.substr(0, i) + " " + ss2 });
				}
			}
			else
			{
				vpis.push_back({ num + atoi(ss2.c_str()), sep2.substr(0, i) + " " + ss2 });
				while (ss2.length()>1 && ss2[0] == '0'){
					ss2 = ss2.substr(1, ss2.length() - 1);
					vpis.push_back({ num + atoi(ss2.c_str()), sep2.substr(0, i) + " " + ss2 });
				}
			}
		}
		else{
			if (sep1 != "")
				dfs(num, sep1 + " " + ss1, ss2);
			else
				dfs(num, ss1, ss2);
		}
	}
}
int main() {
	while (cin >> n >> m, n) {
		vpis.clear();
		dfs(0, "", m);
		bool rej = false;
		bool err = true;
		int ans = 0;
		string anss;
		REP(i, vpis.size()){
			if (ans < vpis[i].first){
				rej = false;
				err = false;
				ans = vpis[i].first;
				anss = vpis[i].second;
			}
			else if (ans == vpis[i].first){
				rej = true;
			}

		}
		if (err)
			cout << "error" << endl;
		else if (rej)
			cout << "rejected" << endl;
		else
			cout << ans << " " << anss << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <vector>

using namespace std;

#define max 6

int main(void){
	int div[] = {1, 10, 100, 1000, 10000, 100000, 1000000 };

	while(1) {
		int t, num, lim, indx = -1, summax = 0;
		bool error = true, rejected = false;
		stringstream ss;
		vector< vector< int > > vs;

		cin >> t >> num;
		ss << num;
		if( !( t | num ) ) break;

		lim = ( 1 << ( ss.str().length() - 1 ) ) - 1;

		for(int i = 0; i <= lim; i++) {
			vector< int > v;
			int temp = 0;
			for(int j = 0; j < max; j++) {
				if( i & ( 1 << j ) ) {
					v.push_back( ( num % div[j + 1] ) / div[temp] );
					temp = j + 1;
				}
			}
			v.push_back( num / div[temp] );
			vs.push_back( v );
		}

		for(int i = 0; i < vs.size(); i++) {
			int sum = 0;
			for(int j = 0; j < vs[i].size(); j++)
				sum += vs[i][j];
			if( sum <= t ) {
				error = false;
				if( sum == summax ) {
					rejected = true;
				} else if( sum > summax ) {
					indx = i;
					summax = sum;
					rejected = false;
				}
			}
		}
		if( error == true ) {
			cout << "error" << endl;
		} else if( rejected == true ) {
			cout << "rejected" << endl;
		} else {
			cout << summax;
			for(int i = vs[indx].size() - 1; i >= 0; i--)
				cout << " " << vs[indx][i];
			cout << endl;
		}
	}

	return 0;
}