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 <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) {
    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()) {
        while(a[ai] == NIL) ai++;
        while(b[bi] == NIL) bi++;

        // cout << "ai:" << ai << ", bi:" << bi << endl;
        if(a[ai] != b[bi]) return 0;

        i = ai + 1;
        ai++; bi++;
    }
    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 && !equal(ans[0], nums)) {
        // cout << "最大値更新:" << maxVal << ", " << equal(ans[0], nums);
        // dump(ans[0]);
        // cout << endl;
        ans.push_back(nums);
    } else if (s > maxVal) {
        ans.clear();
        ans.push_back(nums);
        maxVal = s;
    }

    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());

        // dump(nums);
        // cout << N << ", " << num << ", " << ans.size() << ", " << maxVal << endl;

        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;
}

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

using namespace std;

int main(){

    int t,num;
    string digs;
    while(cin>>t>>digs&&!(t==0&&digs=="0")){
        num=atoi(digs.c_str());
        int len=digs.size();
        int maxCloseNum=-1;
        vector<int> cand;
        bool isRej=false;
        bool isErr=false;
        int cnt=0;
        for(int s = 0; s < (1<<(len-1)); s++){
            vector<int> cuts;
            int start=0;
            for(int j = 0; j < len-1; j++){
                if((s>>j)&1){
                    int a=atoi(digs.substr(start,j+1-start).c_str());
                    start=j+1;
                    cuts.push_back(a);
                }
            }
            cuts.push_back(atoi(digs.substr(start).c_str()));
            int sum=0;
            for(int i = 0; i < cuts.size(); i++){
                sum+=cuts[i];
            }
            if(sum<=t){
                if(maxCloseNum<sum){
                    maxCloseNum=sum;
                    cand=cuts;
                    cnt=0;
                }
                else if(maxCloseNum==sum){
                    cnt++;
                }
            }
        }
        if(maxCloseNum==-1)
            cout<<"error"<<endl;
        else if(cnt!=0)
            cout<<"rejected"<<endl;
        else{
            cout<<maxCloseNum<<" ";
            for(int i = 0; i < cand.size(); i++){
                cout<<cand[i];
                if(cand.size()-1==i)cout<<endl;
                else cout<<" ";
            }
        }
    }

    return 0;
}