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;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

inline int cut(string in,int mask){
	int res=0,cur=0;
	int n=in.size();
	rep(i,n){
		cur=cur*10+(in[i]-'0');
		if(mask&(1<<i))
			res+=cur,cur=0;
	}
	return res+cur;
}

void output(string in,int mask){
	int n=in.size();
	rep(i,n){
		cout << in[i];
		if(mask&(1<<i))
			cout << " ";
	}
	cout << endl;
	return;
}

int main(void){
	int t;
	string in;
	while(cin >> t >> in){
		if(t==0)
			break;
		int n=in.size();
		int cmax=0;
		int num=0;
		int ans=0;
		rep(mask,1<<(n-1)){
			int cur=cut(in,mask);
			if(cur>t)
				continue;
			if(cur>cmax)
				ans=mask,cmax=cur,num=1;
			else if(cur==cmax)
				num++;
		}
		if(num>=2)
			puts("rejected");
		else if(num==1){
			cout << cmax << " ";
			output(in,ans);
		}else
			puts("error");
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int t,num,i,j,f,res,sum,tmp,div;
vector<int>pat,v;
int dcnt(int n){return n?dcnt(n/10)+1:0;}
int main(){
  while(cin>>t>>num&&t){
    f=0,res=-1;
    for(i=0;i<1<<(dcnt(num)-1);i++){
      sum=0,tmp=num,div=10;v.clear();
      for(j=0;j<5;j++)	if((i>>j)&1){ sum+=tmp%div; v.push_back(tmp%div);tmp/=div; div=10;}else div*=10;
      sum += tmp;if(tmp)v.push_back(tmp);
      if(sum<=t)if(res<sum) res=sum,f=0,pat=v;else if(res==sum) f=1;
    }
    if(f) cout<<"rejected";
    else if(res<0) cout<<"error";
    else{ cout<<res;for(i=pat.size()-1;i>=0;i--) cout << " " << pat[i];}
    cout<<endl;
  }
}

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

int t;
string s;

vector<pair<int, vector<int>>> v;

void dfs(int i, int sum, vector<int> vv)
{
	if (sum > t) return;
	
	if (i == s.size()){
		v.push_back(make_pair(sum, vv));
		return;
	}
	
	/*
	
	if (s[i] == '0'){
		vv.push_back(0);
		dfs(i + 1, sum, vv);
		vv.pop_back();
		return;
	}
	
	*/
	
	int a = 0;
	for (int j = i; j < s.size(); j++){
		a *= 10;
		a += s[j] - '0';
		vv.push_back(a);
		dfs(j + 1, sum + a, vv);
		vv.pop_back();
	}
}

void solve()
{
	v.clear();
	dfs(0, 0, vector<int>());
	sort(begin(v), end(v));
	
	if (v.empty()){
		puts("error");
	}
	else if (v.size() >= 2 && v[v.size() - 1].first == v[v.size() - 2].first){
		puts("rejected");
	}
	else {
		printf("%d", v.back().first);
		for (int i = 0; i < v.back().second.size(); i++){
			printf(" %d", v.back().second[i]);
		}
		puts("");
	}
}

int main()
{
	while (cin >> t >> s, t){
		solve();
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <sstream>
#include <deque>

#define REP(i, n) for ( int i = 0; i < n; i++ )
#define NIL -1

using namespace std;

int N, maxVal;
vector<vector<int> > ans;

int getFigure(int num) {
    if(num == 0) return 1;

    int ret = 0;
    while(num > 0) {
        num /= 10;
        ret++;
    }
    return ret;
}

void merge(vector<int> &nums, int idx) {
    int nonZero = 0,
        prevIdx = 0;
    REP(i, nums.size()) {
        if(nums[i] == NIL) continue;
        nonZero++;

        if(nonZero == idx + 2) {
            int step = getFigure(nums[i]);
            REP(j, step) nums[prevIdx] *= 10;
            nums[prevIdx] += nums[i];
            nums[i] = NIL;
            break;
        }
        prevIdx = i;
    }
}

int sum(vector<int> &nums) {
    int ret = 0;
    REP(i, nums.size()) if(nums[i]!=NIL) ret += nums[i];
    return ret;
}

void dump(vector<int> &nums) {
    REP(i, nums.size()) cout << nums[i] << ",";
    // REP(i, nums.size()) if(nums[i]!=NIL)cout << nums[i] << ",";
    cout << endl;
}

bool equal(vector<int> &a, vector<int> &b) {
    int ai = 0, bi = 0;
    REP(i, a.size()) if(a[i] != b[i]) return 0;
    return 1;
}

void solve(vector<int> &nums, int d = 0) {
    int s = sum(nums);

    if(d > nums.size()) return;
    if(s > N) return;

    if (s > maxVal) {
        ans.clear();
        ans.push_back(nums);

        maxVal = s;

    } else if(s == maxVal && !equal(ans[0], nums)) {
        ans.push_back(nums);
    }

    vector<int> base = nums;
    REP(i, nums.size() - (d+1)) {
        // cout << "hoge" << endl;
        merge(nums, i);
        solve(nums, d+1);
        nums = base;
    }
}

int main() {
    int num;
    vector<int> nums;

    while(cin >> N >> num, N||num) {

        maxVal = -1;
        ans.clear();
        nums.clear();

        while(num > 0) {
            nums.push_back(num % 10);
            num /= 10;
        }
        reverse(nums.begin(), nums.end());

        solve(nums);

        if(ans.size() == 0) {
            cout << "error" << endl;
        } else if (ans.size() >= 2) {
            cout << "rejected" << endl;
        } else {
            cout << sum(ans[0]);
            REP(i, ans[0].size()) if(ans[0][i] != NIL) cout << " " << ans[0][i];
            cout << endl;
        }
    }

    return 0;
}