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(n >= M(num,p)){
			cout << M(num,p) << " " << M(num,p) << endl;
			continue;
		}
		
		for(int i = 0; i < p-1; i++){
			//if(num[i+1] == 0 && i != p-1) continue;
			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]);
			}
		}
		for(int i = 0; i < p-2; i++){
			//if(num[i+1] == 0) continue;
			for(int j = i+1; j < p-1; j++){
				//if(num[i+1] == 0 && j != i+1) break;
				//if(num[j+1] == 0 && j != p-2) continue;
				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]);
				}
			}
		}
		for(int i = 0; i < p-3; i++){
			for(int j = i+1; j < p-2; j++){
				//if(num[i+1] == 0 && j != i+1) break;;
				for(int k = j+1; k < p-1; k++){
					//if(num[j+1] == 0 && k != j+1) break;
					//if(num[k+1] == 0 && k != p-2) continue;
					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]);
					}
				}
			}
		}
		for(int i = 0; i < p-3; i++){
			for(int j = i+1; j < p-2; j++){
				//if(num[i+1] == 0 && j != i+1) break;;
				for(int k = j+1; k < p-1; k++){
					//if(num[j+1] == 0 && k != j+1) break;
					for(int l = k+1; l < p-1; l++){
						//if(num[k+1] == 0 && l != k+1) break;
						//if(num[l+1] == 0 && l!= p-2) continue;
						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]);
						}
					}
				}
			}
		}
		if(ans.size() == 0){
			cout << "rejected" << endl;
		} else{
			cout << sum;
			rep(i,ans.size()) cout << " " << ans[i];
			cout << endl;
		}
	}
}

C++: #include<iostream>
#include<vector>
#include<string>
#define F for(int j=0;j<date;j++)
using namespace std;
int main(void){
	int t;
	string s;
	while(1){
		cin>>t>>s;
		if(t==0)break;
		int date=s.size();
		vector<int> a(date,0);
		for(int i=0;i<date;i++){
			a[i]=s[i]-'0';
		}
		int sum=0;
		vector<int> c(date,-1);
		vector<bool> ans(date,false);
		int n=1;
		for(int i=0;i<date;i++){
			n*=2;
		}
		int time=0;
		for(int i=0;i<n;i++){
			int x=i;

			int k=0;
			vector<int> d(date,-1);
			vector<bool> b(date,false);
			F{
				if(x%2==1){
					b[j]=true;
				}
			//	x-=x%2;
				x/=2;
			}
			int num=0,sum1=0;
		//	if(b[date-1])cout<<"ok"<<endl;
			F{
				if(b[j]){
					k*=10;
					k+=a[j];
				}else{
					sum1+=k;
				if(j!=0){
						d[num]=k;
						num++;
				}						
					k=a[j];
					
				}
				
			}
			sum1+=k;
			d[num]=k;
			if(sum1==sum){
				time++;
				//	cout<<sum<<' '<<sum1<<endl;;
	//	F{
	//					if(c[j]!=-1)cout<<c[j];
	//			if(j!=date-1)cout<<' ';
	
		
	//	}
	//	cout<<endl;
			}
			if(sum1<=t&&sum1>sum){
				sum=sum1;
				F{
					ans[j]=b[j];
					c[j]=d[j];
				}
				time=1;


			}
		}
		if(sum==0){
			cout<<"error"<<endl;
			continue;
		}
		if(time>2){
			cout<<"reject"<<endl;
			continue;
		}
		cout<<sum<<' ';
		F{
			//if(!ans[j]){
			//	cout<<a[j];
			//}else{
			//	cout<<a[j];
				if(c[j]!=-1)cout<<c[j];
				if(j!=date-1)cout<<' ';
	
		
		}
		cout<<endl;
	}

	return 0;	
}