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 <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
#include <string>
#include <sstream>
#include <bitset>
using namespace std;
#define INF	100000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define rep(i,n) for(int i = 0; i < n; i++)
#define SORT(x) sort((x).begin(), (x).end())
#define clr(a,b) memset((a),(b),sizeof(a))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<P> Vp;
typedef priority_queue<P, vector<P>, greater<P> > PQ;

bool check(int a, int b, Vi v){
	int sum = 0;
	rep(i,a) sum += v[i];
	if(sum > b) return true;
	else return false;
}

int ipow(int a){
	int ret = 1;
	rep(i,a) ret *= 10;
	return ret;
}

int M(Vi v, int a){
	int ret = 0;
	rep(i,a) ret += v[i]*ipow(a-1-i);
	return ret;
}

int main(){
	int n;
	string str;
	while(cin >> n >> str){
		if(n == 0 && str[0] == '0') break;
		int p = str.size();
		Vi num(p);
		rep(i,p) num[i] = str[i]-'0';
		int sum = -1;
		Vi ans;
		if(check(p,n,num)){
			cout << "error" << endl;
			continue;
		}
		if(p == 6){
			sum = 0;
			rep(i,p){
				sum += num[i];
				ans.pb(num[i]);
			}
		}
		if(n >= M(num,p)){
			cout << M(num,p) << " " << M(num,p) << endl;
			continue;
		}
		
		for(int i = 0; i < p-1; i++){
			int x[2];
			rep(f,2) x[f] = 0;
			for(int f = 0; f <= i; f++) x[0] += num[f]*ipow(i-f);
			for(int f = i+1; f < p; f++) x[1] += num[f]*ipow(p-f-1);
			int tmp = 0;
			rep(f,2) tmp += x[f];
			if(tmp == sum) ans.clear();
			else if(tmp > sum && tmp <= n){
				ans.clear();
				sum = tmp;
				rep(f,2) ans.pb(x[f]);
			}
		}
		//cout << sum << endl;
		for(int i = 0; i < p-2; i++){
			for(int j = i+1; j < p-1; j++){
				int x[3];
				rep(f,3) x[f] = 0;
				for(int f = 0; f <= i; f++) x[0] += num[f]*ipow(i-f);
				for(int f = i+1; f <= j; f++) x[1] += num[f]*ipow(j-f);
				for(int f = j+1; f < p; f++) x[2] += num[f]*ipow(p-f-1);
				int tmp = 0;
				rep(f,3) tmp += x[f];
				if(tmp == sum) ans.clear();
				else if(tmp > sum && tmp <= n){
					ans.clear();
					sum = tmp;
					rep(f,3) ans.pb(x[f]);
				}
			}
		}
		//cout << sum << endl;
		for(int i = 0; i < p-3; i++){
			for(int j = i+1; j < p-2; j++){
				for(int k = j+1; k < p-1; k++){
					int x[4];
					rep(f,4) x[f] = 0;
					for(int f = 0; f <= i; f++) x[0] += num[f]*ipow(i-f);
					for(int f = i+1; f <= j; f++) x[1] += num[f]*ipow(j-f);
					for(int f = j+1; f <= k; f++) x[2] += num[f]*ipow(k-f);
					for(int f = k+1; f < p; f++) x[3] += num[f]*ipow(p-f-1);
					int tmp = 0;
					rep(f,4) tmp += x[f];
					if(tmp == sum) ans.clear();
					else if(tmp > sum && tmp <= n){
						ans.clear();
						sum = tmp;
						rep(f,4) ans.pb(x[f]);
					}
				}
			}
		}
		//cout << sum << endl;
		for(int i = 0; i < p-3; i++){
			for(int j = i+1; j < p-2; j++){
				for(int k = j+1; k < p-1; k++){
					for(int l = k+1; l < p-1; l++){
						int x[5];
						rep(f,5) x[f] = 0;
						for(int f = 0; f <= i; f++) x[0] += num[f]*ipow(i-f);
						for(int f = i+1; f <= j; f++) x[1] += num[f]*ipow(j-f);
						for(int f = j+1; f <= k; f++) x[2] += num[f]*ipow(k-f);
						for(int f = k+1; f <= l; f++) x[3] += num[f]*ipow(l-f);
						for(int f = l+1; f < p; f++) x[4] += num[f]*ipow(p-f-1);
						int tmp = 0;
						rep(f,5) tmp += x[f];
						if(tmp == sum) ans.clear();
						else if(tmp > sum && tmp <= n){
							ans.clear();
							sum = tmp;
							rep(f,5) ans.pb(x[f]);
						}
					}
				}
			}
		}
		//cout << sum << endl;
		if(ans.size() == 0){
			cout << "rejected" << endl;
		} else{
			cout << sum;
			rep(i,ans.size()) cout << " " << ans[i];
			cout << endl;
		}
	}
}

C++: #include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int target, sum;
string num;
vector<int> part;
bool rejected;

int stoi(string s) {
	istringstream is(s);
	int res;
	is >> res;
	return res;	
}

void solve(vector<int>& seq, int idx = 0) {
	if(idx == (int)num.size()) {
		int tmp = 0;
		for(int i = 0; i < (int)seq.size(); ++i)
			tmp += seq[i];

		if(tmp <= target) {
			if(tmp > sum) {
				rejected = false;
				sum = tmp;
				part = seq;
			}
			else if(tmp == sum) {
				rejected = true;
			}
		}
		return;
	}

	seq.push_back(num[idx] - '0');
	solve(seq, idx + 1);
	seq.pop_back();

	for(int i = idx + 1; i < (int)num.size(); ++i) {
		seq.push_back(stoi(num.substr(idx, i - idx + 1)));
		solve(seq, i + 1);
		seq.pop_back();
	}
}

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

	while(cin >> target >> num, target) {
		part.clear();
		rejected = false;
		sum = -1;
		vector<int> tmp;
		solve(tmp);
		if(rejected)
			cout << "rejected" << endl;

		else if(sum == -1)
			cout << "error" << endl;

		else {
			cout << sum << " ";
			for(int i = 0; i < (int)part.size(); ++i)
				cout << part[i] << (i + 1 == (int)part.size() ? "\n" : " ");
		}
	}

	return EXIT_SUCCESS;
}

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

int main()
{
  int X;
  string Y;
  while(cin >> X >> Y, X || Y != "0") {

    int ret = -1, cnt = 0;
    vector< int > ans;

    for(int i = 0; i < 1 << (Y.size() - 1); i++) {
      int now = 0, sum = 0;
      vector< int > nums;
      for(int j = 0; j < Y.size(); j++) {
        now = now * 10 + Y[j] - '0';
        if(j == Y.size() - 1 || (i >> j) & 1) {
          sum += now;
          nums.push_back(now);
          now = 0;
        }
      }
      if(sum <= X && ret <= sum) {
        if(ret == sum) ++cnt;
        else ret = sum, cnt = 1, swap(ans, nums);
      }
    }
    if(cnt == 0) cout << "error" << endl;
    else if(cnt > 1) cout << "rejected" << endl;
    else {
      cout << ret;
      for(int v : ans) cout << " " << v;
      cout << endl;
    }
  }
}