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 <bitset>
#include <cmath>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>


void SplitStringByIndices(const std::string& str, const std::vector<int>& indices, std::vector<std::string>* split_strs) {
  int start_index = 0;
  for (auto index: indices) {
    if (index >= str.size()) {
      break;
    }
    const int length = index - start_index + 1;
    const std::string sub_str = str.substr(start_index, length);
    split_strs->push_back(sub_str);
    start_index += length;
  }
  // rest
  if (start_index < str.size()) {
    const std::string sub_str = str.substr(start_index);
    split_strs->push_back(sub_str);
  }
}

// ex)
// number = 12345
// shred_indices = [1, 3]
// shredded_numbers = [12, 34, 6]
void ShredNumber(int number, const std::vector<int>& shred_indices, std::vector<int>* shredded_numbers) {
  std::string num_str = std::to_string(number);
  std::vector<std::string> split_strs;
  SplitStringByIndices(num_str, shred_indices, &split_strs);

  // string -> int
  for (auto str: split_strs) {
    shredded_numbers->push_back(std::stoi(str));
  }
}

// max_digits <= 32
void NumberToBinaryIndices(int number, int max_digits, std::vector<int>* indices) {
  const std::bitset<32> bits(number);
  const int start_index = 32 - max_digits;
  const std::string bits_str = bits.to_string().substr(start_index);

  for (int i = 0; i < bits_str.size(); ++i) {
    if (bits_str[i] == '1') {
      indices->push_back(i);
    }
  }
}

std::string Solve(int T, int num) {
  const int max_shred = std::to_string(num).size() - 1;
  const int shred_perm_num = std::pow(2, max_shred);

  bool rejected = false;
  int max_under_T = -1;
  std::vector<int> shred_numbers_result;
  for (int i = 0; i < shred_perm_num; ++i) {
    std::vector<int> shred_indices;
    NumberToBinaryIndices(i, max_shred, &shred_indices);
    std::vector<int> shredded_numbers;
    ShredNumber(num, shred_indices, &shredded_numbers);
    const int sum = std::accumulate(shredded_numbers.begin(), shredded_numbers.end(), 0);

    if (sum <= T) {
      if (sum == max_under_T) {  // duplicate
        rejected = true;
      }
      if (sum > max_under_T) {  // max update
        rejected = false;
        max_under_T = sum;
        shred_numbers_result = shredded_numbers;
      }
    }
  }

  if (rejected) {
    return "rejected";
  }

  if (shred_numbers_result.empty()) {
    return "error";
  }

  std::ostringstream oss;
  oss << max_under_T;
  for (int shredded_number: shred_numbers_result) {
    oss << " " << shredded_number;
  }

  return oss.str();
}


int main() {
  struct Test {
    int t;
    int num;
  };

  std::vector<Test> tests;
  for (;;) {
    int t, num;
    std::cin >> t >> num;
    if (t == 0 && num == 0) break;
    Test test = {t, num};
    tests.push_back(test);
  }

  for (Test test: tests) {
    std::cout << Solve(test.t, test.num) << std::endl;
  }

  return 0;
}

C++: #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>

using namespace std;

#define INF 999999999
#define REP(i,n) for(int i=0; i<(int)(n); i++)

int main(){
	int i, j, t;
	string in;
	int ans, sum, now, cut;
	while(1){
		cin >> t >> in;
		if(!t) break;
		ans = 1000000;
		for(i=1;i<(1<<in.size());i+=2){
			sum = 0;
			now = 0;
			REP(j,in.size()){
				if(!(i&1<<(in.size()-j-1))){
					now *= 10;
					now += in[j] - '0';
				}
				else{
					now *= 10;
					now += in[j] - '0';
					sum += now;
					now = 0;
				}
			}
			if(sum == abs(ans)) ans = -abs(sum);
			else if(sum<=t&&abs(sum-t)<=abs(abs(ans)-t)){
				ans = sum;
				cut = i;
			}
		}
		if(ans < 0) cout << "rejected" << endl;
		else if(ans == 1000000) cout << "error" << endl;
		else{
			//cout << cut << " ";
			cout << ans << " ";
			for(j=in.size()-1;j>=0;j--){
				cout << in[in.size()-j-1];
				if(j>=1&&(cut&1<<j)) cout << " ";
			}
			cout << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
void Cut(int);
void solve();
int t,len,flag,flag2,ans,ansc[6],anss;
string num;
bool cut[6];

int main(){
  while(1){
    cin>>t>>num;
    if(!t&&num=="0") break;
    for(int i=0;i<6;i++) cut[i]=false;
    len=num.size();
    flag=flag2=0;
    ans=-1;
    Cut(0);
    if(!flag) cout<<"error"<<endl;
    else{
      if(flag2) cout<<"rejected"<<endl;
      else{
	cout<<ans<<' ';
	for(int i=0;i<anss;i++) cout<<ansc[i]<<' ';
	cout<<endl;
      }
    }
  }
  return 0;
}

void Cut(int x){
  if(x==len-1){
    solve();
    return;
  }
  for(int i=0;i<2;i++) cut[x]=i,Cut(x+1);
}

void solve(){
  int sum=0,value=0,ind=0,v[6],f=0;
  for(int i=0;i<len;i++){
    value=value*10+(num[i]-'0');
    if(cut[i]) sum+=value,value=0;
  }
  sum+=value;
  if(sum<=t&&sum>=ans){
    value=0;
    flag=1;
    if(ans==sum) flag2=1;
    else flag2=0;
    ans=sum;
    for(int i=0;i<len;i++){
      value=value*10+(num[i]-'0');
      if(cut[i]) v[ind]=value,value=0,ind++;
    }
    v[ind]=value;
    ind++;
    anss=ind;
    for(int i=0;i<ind;i++) ansc[i]=v[i];
  }
}

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

using namespace std;
typedef pair<int,int> P;
int t, in, lim;
P ans;
string ans_s;

string change(string s, int n){
  int a = 0;
  string out = "";

  for(int i = 0; i < s.length(); i++){
    out += s[i];
    if(s[i] >= '0' && s[i] <= '9') a++;
    if(a == n) out += " ";
  }
  return out;
}


int make(string s){
  s += " ";
  int out = 0;
  int now = 0;

  for(int i = 0; i < s.length(); i++){
    if(s[i] < '0' || s[i] > '9'){
      out += now;
      now = 0;
    }else{
      now *= 10;
      if(now == 0 && s[i] == '0') return -1;
      now += s[i]-'0';
    }
  }
  return out;
}

void solve(string s, int num){

  int dig = make(s);
  if(dig != -1){
    if(dig <= t && dig >= ans.first){ 
      if(dig == ans.first && ans_s != s) ans.second++;
      else{
	ans.first = dig;
	ans.second = 1;
	ans_s = s;
      }
    }
  }
  if(num < lim){
    //do not cut 
    solve(s,num+1);
    //cut
    s = change(s,num);
    solve(s,num+1);
  }
}

bool isError(string s){
  int n = 0;
  for(int i = 0; i < s.length(); i++)
    n+=s[i]-'0';
  
  return t<n;
}

string tos(int num){
  string out = "";
  while(num){
    out += num%10+'0';
    num/=10;
  }
  reverse(out.begin(),out.end());
  return out;
}


int main(){
  while(cin >> t >> in && in){
    ans = P(-1,2);
    string s = tos(in);
    if(isError(s)) cout << "error" << endl;
    else{
      lim = s.length();
      solve(s,1);
      if(ans.second > 1) cout << "rejected" << endl;
      else cout << ans.first << " " << ans_s << endl;
    }
  }

  return 0;
}