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>
#include <algorithm>
using namespace std;


int mypow(int x, int y) {
    int ret = 1;
    while(y > 0) {
        ret *= x;
        y--;
    }
    return ret;
}
int main() {
    int t, num;
    while(cin >> t >> num, t) {
        vector<vector<vector<int>>> ret(1000000);
        ret[num].push_back({num});

        for(int i=1; i<(1<<5); ++i) {
            int top = 0;
            for(int j=4; j>=0; --j) {
                if((i >> j) % 2 == 1) {
                    top = j;
                    break;
                }
            }
            if(mypow(10, top+1) > num) {
                break;
            }
            int rest = num;
            vector<int> v2;
            int sum=0;
            int prev_b = -1;
            for(int b=0; b<5; ++b) {
                if((i >> b) % 2 == 1) {
                    int tmp = 0;
                    if(prev_b != -1) {
                        tmp = mypow(10, b - prev_b);
                    } else {
                        tmp = mypow(10, b+1);
                    }
                    v2.push_back(rest % tmp); // todo
                    sum += rest % tmp;
                    rest /= tmp;
                    prev_b = b;
                    if(rest == 0) {
                        break;
                    }
                }
            }
            if(rest != 0) {
                v2.push_back(rest);
            }
            sum += rest;
            //cout << "rest = " << rest << "   sum=" << sum << endl;
            //cout << endl;
            //for(auto x : v2) {
            //    cout << x << endl;
            //}
            //cout << endl;
            if(sum == 0) {
                continue;
            }
            ret[sum].push_back(v2);
        }

        bool finded = false;
        for(int i=t; i>=0; --i) {
            if(ret[i].size() >= 2) {
                cout << "rejected" << endl;
                finded = true;
                break;
            } else if(ret[i].size() == 1) {
                finded = true;
                cout << i << " ";
                reverse(ret[i][0].begin(), ret[i][0].end());
                for(int j=0; j<ret[i][0].size()-1; ++j) {
                    cout << ret[i][0][j] << " ";
                }
                cout << ret[i][0][ret[i][0].size()-1] << endl;
                break;
            }
        }
        if(!finded) {
            cout << "error" << endl;
        }
    }
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
#include<queue>
#include<stack>
#include<set>
using namespace std;
int stoi(string x){stringstream ss;ss<<x;int tmp;ss>>tmp;return tmp;}
string itos(int x){stringstream ss;ss<<x;return ss.str();}
#define sz(n)  (int)n.size()
#define dump(x) cerr<<#x<<" = "<<(x)<<endl;
#define debug(x)cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define MP make_pair
#define PB push_back
//	priority_queue<int,vector<int>,greater<int>() >ta;
	int n,m;
	string sn,sm;
	int ans=0;
	bool used[1000000];
	bool flag[100];
	vector<string>ab;
	void dfs(int h,int f){
		if(h==sz(sm)){
		//for(int i=0;i<sz(sm);i++)cout<<flag[i]<<" ";cout<<endl;
		int sum=0;
		string tmp="";
		flag[sz(sm)-1]=0;
		for(int i=0;i<sz(sm);i++){
			tmp.push_back(sm[i]);
			if(flag[i])sum+=stoi(tmp),tmp="";
		}
		if(sz(tmp)>=1)sum+=stoi(tmp);
		if(f==1&&sum==ans){
			//for(int i=0;i<sz(sm);i++)cout<<flag[i]<<" ";cout<<endl;
			tmp="";
			for(int i=0;i<sz(sm);i++){
			tmp.push_back(sm[i]);
			if(flag[i]){ab.push_back(tmp),tmp="";}
		}
		if(!tmp.empty())ab.push_back(tmp);
		return ;
		}
		
		
		if(sum<=n)ans=max(ans,sum);
		used[sum]++;
		//for(int i=0;i<sz(sm);i++)cout<<flag[i]<<" ";cout<<sum<<endl;
		return ;
		}
		
		flag[h]=1;
		dfs(h+1,f);
		flag[h]=0;
		if(h+1<sz(sm))dfs(h+1,f);
	}
	
	int main(){
	while(cin>>n>>m,n||m){
	sn=itos(n),sm=itos(m);
	fill(used,used+1000000,0);
	fill(flag,flag+100,0);
	ans=0;
	ab.clear();
	dfs(0,0);
	if(used[ans]>=2)cout<<"rejected"<<endl;
	else if (ans==0)cout<<"error"<<endl;
	else {
		cout<<ans<<" ";
		dfs(0,1);
		for(int i=0;i<sz(ab);i++){cout<<ab[i];(i==sz(ab)-1)?cout<<endl:cout<<" ";}
	}
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define INF 1e9

int t,ans,size,diff;
vector<int> a,path;
map<int,int> mp;

int calc(int idx,int N){
  int res = 0, p = 1;
  for(int i = N-1 ; i >= idx ; i--){
    res += a[i]*p;
    p *= 10;
  }
  return res;
}

void solve(int idx,int val,vector<int> &v){
  if(idx == size){
    if(val > t) return;
    if(t-val <= diff){
      diff = t-val;
      ans = val; mp[ans]++;
      path = v;
    }
    return;
  }

  for(int i = 1 ; i <= size ; i++){
    if(idx + i > size) continue;
    int c = calc(idx,idx+i);
    v.push_back(c);
    solve(idx+i,val+c,v);
    v.pop_back();
  }
}

int main(){
  int num;
  while(cin >> t >> num, (t | num)){
    mp.clear(); a.clear(); 
    diff = ans = INF;
    for(int i = 0 ; ; i++){
      if(num == 0) break;
      a.push_back(num%10);
      num /= 10;
    }
    reverse(a.begin(),a.end());
    size = a.size();
    vector<int> v;
    solve(0,0,v);
    if(diff == INF){
      cout << "error" << endl;
    }else{
      if(mp[ans] != 1){
        cout << "rejected" << endl;
      }else{
        cout << ans;
        for(int i = 0 ; i < (int)path.size() ; i++){
          cout << " " << path[i];
        }
        cout << endl;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>

using namespace std;

int dp[7][1000000];
int pr[7][1000000];
int df[7][1000000];

int main(){
	while(true){
		int n;
		string s;
		cin >> n >> s;
		if(n == 0 && s == "0"){ break; }
		const int m = s.size();
		for(int i = 0; i <= m; ++i){
			for(int j = 0; j <= n; ++j){
				dp[i][j] = pr[i][j] = df[i][j] = 0;
			}
		}
		dp[0][0] = 1;
		for(int i = 0; i < m; ++i){
			int x = 0;
			for(int j = i; j < m; ++j){
				x = (x * 10) + s[j] - '0';
				if(x > n){ break; }
				for(int k = 0; k + x <= n; ++k){
					if(dp[i][k] == 0){ continue; }
					dp[j + 1][k + x] += dp[i][k];
					pr[j + 1][k + x] = i;
					df[j + 1][k + x] = x;
				}
			}
		}
		int answer = -1;
		for(int i = n; answer < 0 && i >= 0; --i){
			if(dp[m][i] != 0){ answer = i; }
		}
		if(answer < 0){
			cout << "error" << endl;
		}else if(dp[m][answer] >= 2){
			cout << "rejected" << endl;
		}else{
			cout << answer;
			vector<int> cut;
			int r = m, c = answer;
			do {
				const int d = df[r][c];
				cut.push_back(d);
				r = pr[r][c];
				c -= d;
			} while(r > 0);
			for(int i = cut.size() - 1; i >= 0; --i){
				cout << " " << cut[i];
			}
			cout << endl;
		}
	}
	return 0;
}