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++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

void foo(string s, vector<string> w, vector< vector<string> >& v) {
	if (s.empty()) {
		v.push_back(w);
		return;
	}
	for (int l = 1; l <= s.length(); l++) {
		vector<string> _w = w;
		_w.push_back(s.substr(0, l));
		foo(s.substr(l), _w, v);
	}
}

int main() {
	for (;;) {
		int t; string num; cin >> t >> num;
		if (t == 0) break;
		vector< vector<string> > v;
		foo(num, vector<string>(), v);
		int _i, maxi = -1;
		bool rejected = false;
		for (int i = 0; i < v.size(); i++) {
			int sum = 0;
			for (int j = 0; j < v[i].size(); j++) {
				stringstream ss(v[i][j]);
				int x; ss >> x;
				sum += x;
			}
			if (sum > t) continue;
			if (sum > maxi) {
				_i = i;
				maxi = sum;
				rejected = false;
			}
			else if (sum == maxi)
				rejected = true;
		}
		if (maxi == -1) cout << "error" << endl;
		else if (rejected) cout << "rejected" << endl;
		else {
			cout << maxi;
			for (int j = 0; j < v[_i].size(); j++)
				cout << ' ' << v[_i][j];
			cout << endl;
		}
	}
}

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

using namespace std;

int main(void)
{
	int t;
	string num;
	string temp;
	int x;
	int sum,Asum;
	int minn;
	int eq;
	int i1;

	while(1){
		cin>>t>>num;
		if(t==0 && num=="0") break;

		vector<int> v(num.size()+1,0);
		vector<int> Avec;
		minn=1000000;
		eq=-1;

		while(1){
			temp=num[0];
			sum=0;

			for(i1=1;i1<num.size();i1++){
				if(v[i1]==0){
					temp+=num[i1];
				}
				if(v[i1]==1){
					istringstream istr(temp);
					istr >> x;
					sum+=x;
					temp=num[i1];
				}
			}

			istringstream istr(temp);
			istr >> x;
			sum+=x;

			if(t>=sum){
				x=t-sum;
				if(minn==x) eq=x;
				else if(minn>x) {
					minn=x;
					Asum=sum;
					Avec.clear();
					Avec.reserve(v.size());
					copy(v.begin(),v.end(),back_inserter(Avec));
				}	
			}

			v[1]++;
			for(i1=1;i1<num.size();i1++){
				if(v[i1]==2) {
					v[i1]=0;
					v[i1+1]++;
				}
			}
			if(v[num.size()]==1) break;
		}

		if(minn==eq) cout<<"rejected"<<endl;
		else if(minn==1000000) cout<<"error"<<endl;
		else {
			cout<<Asum<<" ";
			temp=num[0];
			for(i1=1;i1<num.size();i1++){
				if(Avec[i1]==0){
					temp+=num[i1];
				}
				if(Avec[i1]==1){
					cout<<temp<<" ";
					temp=num[i1];
				}
			}
			cout<<temp<<endl;
		}

		
	}

	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(vector<int>::iterator itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define MP make_pair

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

int t, max_num, f = 0;
vector<int> ans;

int get_nextnum(int x){
  char s[10];
  sprintf(s, "%d", x);
  return s[0] + '0';
}

int calc_digit(int x){
  int res = 0;
  while(x != 0){
    x /= 10;
    res++;
  }
  return res;
}

void solve(vector<int> v, int digit, int n, int num){
  if(n == digit){
    int sum = 0;
    v.pb(num);
    REPI(itr,v){
      sum += *itr;
    }
    if(t >= sum && sum == max_num){
      f = 1;
    }else if(t >= sum && sum > max_num){
      max_num = sum;
      f = 0;
      ans.clear();
      copy(v.begin(), v.end(), back_inserter(ans));
    }
    return;
  }
  
  solve(v, digit, n + 1, num);
  if(get_nextnum(num) != 0){
    int tmp = 1;
    REP(i,digit - n){
      tmp *= 10;
    }
    v.pb(num / tmp);
    solve(v, digit, n + 1, num % tmp);
  }
}

int main(){

  while(true){
    int num;
    scanf("%d %d", &t, &num);
    
    if(t == 0 && num == 0)  break;
    if(t == num){
      printf("%d %d\n", t, num);
      continue;
    }
    int digit = calc_digit(num);
    max_num = -1, f = 0;
    vector<int> v;
    solve(v, digit, 1, num);
    if(max_num == -1){
      printf("error\n");
    }else if(f == 1){
      printf("rejected\n");
    }else{
      printf("%d ", max_num);
      REPI(itr, ans){
        printf("%d", *itr);
        vector<int>::iterator itrt = itr;
        itrt++;
        if(itrt != ans.end()){
          printf(" ");
        }
      }
      printf("\n");
    }
  }
  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;
}