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<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<<"\n":cout<<" ";}
	}
	}
	return 0;
}

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

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main(){
  int t;
  string s;
  while(cin>>t>>s && t){
    bool reject = false;
    int ans = -1;
    vector<int> ans_v;
    for(int S = 0; S < (1<<(s.size() - 1)); S++){
      int sum = 0;
      int tmp = 0;
      vector<int> tmp_v;
      REP(i, s.size()){
        tmp = tmp * 10 + (s[i] - '0');
        if(i == s.size() - 1 || (S & (1<<i))){
          tmp_v.push_back(tmp);
          sum += tmp;
          tmp = 0;
        }
      }
      if(sum > t){
      }else if(ans < sum){
        reject = false;
        ans = sum;
        ans_v = tmp_v;
      }else if(ans == sum){
        reject = true;
      }
    }
    if(reject){
      cout<<"rejected"<<endl;
    }else if(ans == -1){
      cout<<"error"<<endl;
    }else{
      cout<<ans<<" ";
      debug(ans_v.begin(), ans_v.end());
    }
  }
  return 0;
}

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

int main(){
  while(1){
    int t,n,len;
    string str;
    cin >> t >> str;
    if(t==0) break;
    len = str.length();
    n = atoi(str.c_str());
    
    int ans=0;
    int idx=-1;
    bool rej=false;
    for(int i=0; i<(1<<(len-1)); i++){
      int num=n;
      int sum=0;
      for(int j=len-1; j>0; j--){
        if((i & 1<<(j-1)) != 0){
          int d = num/pow(10,j);
          sum += d;
          num -= d*pow(10,j);
        }
      }
      sum+=num;
      if(sum<=t){
        if(ans==sum){
          rej=true;
        }else if(sum > ans){
          ans = sum;
          idx=i;
          rej=false;
        }
      }
    }
    if(rej){
      cout << "rejected" << endl;
      continue;
    }
    if(ans==0){
      cout << "error" << endl;
      continue;
    }
    cout << ans << " ";
    for(int j=len-1; j>0; j--){
      if((idx& 1<<(j-1)) != 0){
        int d = n/pow(10,j);
        cout << d << " ";;
        n -= d*pow(10,j);
      }
    }
    cout << n << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int target;
int ans,anscnt;
vector <int> ans2;
vector <int> n;
int flg[7][1000001];
void saiki(int ima,int sum){
  int t = 0;
  if(ima == n.size()){
    if(sum <= target && ans < sum) ans = sum,anscnt = 1,flg[ima][sum] = 1,ans2.clear();
  else if(ans == sum) anscnt++;
  }

  if(sum<=target){
    for(int i=ima;i<n.size();i++){
      t*=10;
      t+=n[i];
      saiki(i+1,sum+t);
      if(flg[i+1][sum+t] ==1) flg[ima][sum] = 1,ans2.push_back(t),flg[i+1][sum+t] = 0;
    }
  }

  if(ima == 0) flg[ima][sum] = 0;
}



int main() {

  while(1) {
    int a;
    cin >> target >> a;
    if(target == 0 && a == 0 ) break;
    
    int c = 0;
    n.clear();
    while(a != 0){
      n.push_back(a%10);
      a /= 10;
    }
    reverse(n.begin(),n.end());
    ans = -1;
    anscnt = 0;
    ans2.clear();
    saiki(0,0);
    if(anscnt >=2) cout << "rejected"<<endl;
    else if(ans == -1) cout << "error"<<endl;
    else {
      cout << ans <<" ";
      for(int i=0;i<ans2.size();i++){
	cout << ans2[ans2.size()-1-i];
	if(i != ans2.size()-1) cout <<" ";
      }
	cout << endl;
    }
  }

    return 0;
}