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 n, m;
    
    while (scanf("%d %d", &n, &m) && n){
        int ctr = 0;
        char tmp[64];
        sprintf(tmp, "%d", m);
        string s = tmp;
        
        for (int x = m; x; x /= 10) ctr++;
        
        vector<pair<int, int> > v;
        for (int i = 0; i < 1 << (ctr - 1); i++){
            int p = 0;
            int sum = 0;
            for (int j = 0; j < ctr - 1; j++){
                if (i >> j & 1){
                    string s2 = s.substr(p, j + 1 - p);
                    sum += atoi(s2.c_str());
                    p = j + 1;
                }
            }
            
            sum += atoi(s.substr(p, ctr - p).c_str());
            v.push_back(make_pair(sum, i));
        }
        
        sort(v.begin(), v.end());
        
        if (v[0].first > n){
            printf("error\n");
        }
        else {
            int minp = 0, ct = 1;
            for (int i = 1; i < v.size(); i++){
                if (v[i].first <= n && v[minp].first <= v[i].first){
                    if (v[minp].first == v[i].first){
                        ct++;
                    }
                    else {
                        minp = i;
                        ct = 1;
                    }
                }
            }
            if (ct > 1){
                printf("rejected\n");
            }
            else {
                printf("%d ", v[minp].first);
                int p = 0;
                for (int j = 0; j < ctr - 1; j++){
                    if (v[minp].second >> j & 1){
                        string s2 = s.substr(p, j + 1 - p);
                        printf("%s ", s2.c_str());
                        p = j + 1;
                    }
                }
                
                printf("%s\n", s.substr(p, ctr - p).c_str());
            }
        }
    }
    
    return (0);
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
string s;
int n;
vector<pair<int,string> >ans;
void f(int i,int sum,string t)
{
	if(sum>n)return;
	else if(i==s.size())
	{
		ans.push_back(make_pair(sum,t));
		return;
	}
	for(int j=1;i+j<=s.size();j++)
	{
		f(i+j,sum+stoi(s.substr(i,j)),t+(i==0?"":" ")+s.substr(i,j));
	}
}
main()
{
	while(cin>>n>>s,n||s!="0")
	{
		ans.clear();
		f(0,0,"");
		if(ans.size()==0)cout<<"error"<<endl;
		else
		{
			sort(ans.begin(),ans.end());
			if(ans.size()>1&&ans[ans.size()-1].first==ans[ans.size()-2].first)cout<<"rejected"<<endl;
			else cout<<ans[ans.size()-1].first<<" "<<ans[ans.size()-1].second<<endl;
		}
	}
}