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 <vector>
using namespace std;

vector<int> solve(int t, string num, vector<int> ret, int i, int k){
  if(i == -1) return ret;
  bool f = false, g = false;
  vector<int> r1, r2;
  if(ret[0] != -1 && ret[0] + k*10*(num[i]-'0') <= t){
    ret[ret.size()-1] += k*10*(num[i]-'0');
    ret[0] += k*10*(num[i]-'0');
    r1 = solve(t,num,ret,i-1,k*10);
    ret[ret.size()-1] -= k*10*(num[i]-'0');
    ret[0] -= k*10*(num[i]-'0');
    f = (r1[0] != -1);
  }
  if(ret[0] != -1 && ret[0] + (num[i]-'0') <= t){
    ret.push_back(num[i]-'0');
    ret[0] += (num[i]-'0');
    r2 = solve(t,num,ret,i-1,1);
    ret.pop_back();
    ret[0] -= (num[i]-'0');
    g = (r2[0] != -1);
  }
  if(f && g){
    if(r1[0] == r2[0]){
      return (vector<int>){r1[0],-1};
    }else{
      return max(r1,r2);
    }
  }
  if(f) return r1;
  if(g) return r2;
  return vector<int>(1,-1);
}

int main(){
  int t;
  string num;
  while(cin >> t >> num, t){
    int n = num.length();
    vector<int> init(1,num[n-1]-'0');
    init.push_back(num[n-1]-'0');
    vector<int> ans = solve(t,num,init,n-2,1);
    if(ans[0] == -1) cout << "error" << endl;
    else if(ans[1] == -1) cout << "rejected" << endl;
    else{
      cout << ans[0];
      for(int i = ans.size()-1; i > 0; --i)
        cout << " " << ans[i];
      cout << endl;
    }
  }
  return 0;
}

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

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

typedef vector<int> vec;
typedef vector<vec> mat;

const int INF = 1 << 28;
int sum;
string num;

int main() {
    while (cin >> sum >> num, sum || num != "0") {
        map<int,vec> m;
        int target = -1;
        for (int i = 0; i < 1 << (num.size() - 1); i++) {
            int tmp = 0;
            int pre = 0;
            for (int j = 0; j < num.size() - 1; j++) {
                if (i & (1 << j)) {
                    string s = string(num.begin() + pre, num.begin() + j + 1);
                    if (s[0] == '0' && s.size() > 1) continue;
                    stringstream ss; ss << s;
                    int n; ss >> n;
                    tmp += n;
                    pre = j + 1;
                }
            }

            string s = string(num.begin() + pre, num.end());
            stringstream ss; ss << s;
            int n; ss >> n;
            tmp += n;

            if (tmp <= sum) {
                m[tmp].push_back(i);
                target = max(target, tmp);
            }
        }

        if (m.size() == 0) {
            cout << "error" << endl;
        } else if (m[target].size() == 1) {
            cout << target << " ";
            int tmp = m[target][0];
            for (int i = 0; i < num.size(); i++) {
                cout << num[i];
                if (tmp & (1 << i)) cout << " ";
            }
            cout << endl;
        } else {
            cout << "rejected" << endl;
        }
    }
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;

bool flag;
int res,cnt,t;
int resn[6];
int num[6];
string str;

void dfs(int v,int val,int cut){
	if(v==str.size()){
		if(val<=t){
			if(res==val)flag=true;
			if(res<val){
				flag=false;
				res=val;
				cnt=cut;
				for(int i=0;i<cnt;i++){
					resn[i]=num[i];
				}
			}
		}
		return;
	}
	int pp=0;
	for(int i=v;i<str.size();i++){
		pp=pp*10+str[i]-'0';
		num[cut]=pp;
		dfs(i+1,val+pp,cut+1);
	}
}

int main(void){
	while(1){
		cin >> t >> str;
		if(t==0 && str=="0")break;
		res=0;
		flag=false;
		dfs(0,0,0);
		if(res==0)printf("error\n");
		else if(flag)printf("rejected\n");
		else{
			printf("%d ",res);
			for(int i=0;i<cnt;i++){
				printf("%d%c",resn[i],i==cnt-1?'\n':' ');
			}
		}
	}
	return 0;
}