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;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

    int t;
    string s;
    while (cin >> t >> s, t) {
        int n = s.size();
        map<int, int> mp;   // sum, num
        map<int, V> mp2;    // sum, sep
        rep2(state, 1 << (n - 1), 1 << n) {
            int x = 0;
            int temp = 0;
            V v;
            rep(i, n) {
                temp *= 10;
                temp += s[i] - '0';
                if ((state >> i) & 1) {
                    x += temp;
                    v.emplace_back(temp);
                    temp = 0;
                }
            }
            mp[x]++;
            mp2[x] = v;
        }

        int ma_val = -1;
        int ma_num = -1;
        V v;
        for (auto p : mp) {
            if (p.first <= t) {
                ma_val = p.first;
                ma_num = p.second;
                v = mp2[p.first];
            }
        }

        if (ma_val == -1) {
            cout << "error" << endl;
        } else if (ma_num > 1) {
            cout << "rejected" << endl;
        } else {
            cout << ma_val;
            for (auto x : v) cout << " " << x;
            cout << endl;
        }
    }
}

C++: #include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;

int main(){
    while(1){
        string s;
        int t;
        cin >> t >> s;
        if(t==0) break;
        
        int num = atoi(s.c_str());
        int len = s.length();
        int ans=-1, smax=0;
        bool rejected = false;
        for(int i=0; i< (1<<(len-1)); i++){
            int n = num;
            int sum = 0;
            for(int j=0; j<len; j++){
                if((1<<j) & i){
                    int sep = n/pow(10,len-j-1);
                    sum += sep;
                    n -= sep*pow(10,len-j-1);
                }
            }
            sum += n;
            if(sum==smax){
                rejected = true;
            }
            if(sum<=t && sum>smax){
                smax = sum;
                ans = i;
                rejected = false;
            }
        }

        if(ans == -1){
            cout << "error" << endl;
        }else if(rejected){
            cout << "rejected" << endl;
        }else{
            cout << smax << " ";
            for(int i=0; i<len; i++){
                cout << s[i];
                if((1<<i) & ans) cout << " ";
            }
            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<<"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!=date-1)cout<<' ';
	
		
		}
		cout<<endl;
	}

	return 0;	
}

C++: #include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1000000000

using namespace std;
typedef pair<llint, llint> P;

string s;
llint t;
vector<P> vec;

int main(void)
{
  while(1){
    cin >> t >> s;
    if(s=="0" && t==0) break;
    reverse(s.begin(), s.end());
    vec.clear();

    llint n = s.size(), N = 1<<(n-1);
    for(int i = 0; i < N; i++){
      llint sum = 0, mul = 1;
      for(int j = 0; j < n; j++){
        sum += mul*(s[j]-'0');
        mul *= 10;
        if(j < n-1 && (i & (1<<j))) mul = 1;
      }
      vec.push_back(P(sum, i));
    }
    sort(vec.begin(), vec.end());

    /*for(int i = 0; i < vec.size(); i++){
      cout << vec[i].first << " " << vec[i].second << endl;
    }*/

    llint p = upper_bound(vec.begin(), vec.end(), P(t, inf)) - vec.begin();
    p--;
    if(p < 0){
      cout << "error" << endl;
      continue;
    }
    if(p > 0 && vec[p-1].first == vec[p].first){
      cout << "rejected" << endl;
      continue;
    }
    cout << vec[p].first << " ";

    llint cut = vec[p].second, sum = 0, mul = 1;
    vector<llint> ans;
    for(int j = 0; j < n; j++){
      sum += mul*(s[j]-'0');
      mul *= 10;
      if(j < n-1 && (cut & (1<<j))){
        mul = 1;
        ans.push_back(sum);
        sum = 0;
      }
    }
    ans.push_back(sum);

    reverse(ans.begin(), ans.end());
    for(int i = 0; i < ans.size(); i++){
      cout << ans[i];
      if(i < (int)ans.size()-1) cout << " ";
    }
    cout << endl;
  }

  return 0;
}