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;


int main(){
    int t;
    string num;
    while(cin>>t>>num,t!=0 || num!="0"){
        vector<vector<int>> dp(num.size()+1,vector<int>(t+1,0));
        dp[0][0]=1;
        for(int i=0;i<num.size();i++){
            for(int j=0;j<=t;j++){
                for(int k=i+1;k<=num.size();k++){
                    //cut num[i,k)
                    int cutnum=stoi(num.substr(i,k-i));
                    if(cutnum+j<=t) dp[k][j+cutnum]+=dp[i][j];
                }
            }
        }
        for(int i=t;i>=-1;i--){
            if(i==-1){
                cout<<"error"<<endl;
                break;
            }
            if(dp[num.size()][i]){
                if(dp[num.size()][i]>=2){
                    cout<<"rejected"<<endl;
                }
                else{
                    vector<string> res;
                    int pos=num.size();
                    int val=i;
                    while(pos>0){
                        for(int j=0;j<pos;j++){
                            //cut num[j,pos)
                            int v=stoi(num.substr(j,pos-j));
                            if(val-v>=0 && dp[j][val-v]==1){
                                res.push_back(num.substr(j,pos-j));
                                pos=j;
                                val-=v;
                                break;
                            }
                        }
                    }
                    cout<<i<<" ";
                    reverse(res.begin(),res.end());
                    for(int j=0;j<res.size();j++){
                        cout<<res[j] <<(j+1==res.size() ? "\n" : " ");
                    }
                }
                break;    
            }
        }
        
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;

int conv_stoi(string s) {
    int res = 0, pow_ten = 1;
    for (int i = s.size() - 1; i >= 0; i--) {
        res += int(s[i] - '0') * pow_ten;
        pow_ten *= 10;
    }
    return res;
}

void solve(int t, int num) {
    string number;
    int tmp = num;
    int n = 0;
    while (tmp > 0) {
        n++;
        tmp /= 10;
    }
    for (int i = 0; i < n; i++) {
        number += char(num % 10 + '0');
        num /= 10;
    }
    reverse(number.begin(), number.end());
    map<int, int> cnt, number_mask;
    int ans = -1;
    for (int mask = 0; mask < (1 << n); mask++) {
        if (!(mask & 1)) continue;
        vector<int> sep;
        for (int i = 0; i < number.size(); i++) {
            if ((mask >> i) & 1) sep.push_back(i);
        }
        int m = sep.size();
        sep.push_back(number.size());
        int sum = 0;
        vector<int> numbers;
        for (int i = 0; i < m; i++) {
            int now_number = conv_stoi(number.substr(sep[i], sep[i + 1] - sep[i]));
            numbers.push_back(now_number);
            sum += now_number;
        }
        if (sum > t) continue;
        cnt[sum]++;
        ans = max(ans, sum);
        number_mask[sum] = mask;
    }
    if (ans < 0) cout << "error" << endl;
    else if (cnt[ans] > 1) cout << "rejected" << endl;
    else {
        cout << ans << ' ';
        int ans_mask = number_mask[ans];
        vector<int> ans_sep;
        for (int i = 0; i < n; i++) {
            if ((ans_mask >> i) & 1) ans_sep.push_back(i);
        }
        int m = ans_sep.size();
        ans_sep.push_back(n);
        for (int i = 0; i < m; i++) {
            cout << conv_stoi(number.substr(ans_sep[i], ans_sep[i + 1] - ans_sep[i])) << " \n"[i == m - 1];
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t, num;
    while (true) {
        cin >> t >> num;
        if (t == 0 && num == 0) break;
        solve(t, num);
    }
    return 0;
}

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

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

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

int main() {
    while (cin >> sum >> num, sum || num != "0") {
        map<long long int,vec> m;
        long long int target = -1;
        for (int i = 0; i < 1 << (num.size() - 1); i++) {
            long long 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);
                    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 << "reject" << 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,kazu=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;
				kazu=0;
				F{
					if(d[j]!=-1)kazu++;
					ans[j]=b[j];
					c[j]=d[j];
				}
				time=1;


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

	return 0;	
}