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) {
    // start with 0 ?
    /*
    if (str != "0" && str[0] == '0') {
      shredded_numbers->clear();
      return;
    }
    */
    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);

  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 !
        return "rejected";
      }
      if (sum >= max_under_T) {
        max_under_T = sum;
        shred_numbers_result = shredded_numbers;
      }
    }
  }

  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;
  };

  const std::vector<Test> tests = {
    {50, 12346},
    {376, 144139},
    {927438, 927438},
    {18, 3312},
    {9, 3142},
    {25, 1299},
    {111, 33333},
    {103, 862150},
    {6, 1104}
  };

  for (Test test: tests) {
    std::cout << Solve(test.t, test.num) << std::endl;
  }
  */
  for (;;) {
    int t, num;
    std::cin >> t;
    std::cin >> num;
    if (t == 0 && num == 0) break;

    std::cout << Solve(t, num) << std::endl;
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<cmath>
using namespace std;
typedef long long int ll;
ll number(int b,int e,string s){
	if(b==s.size())return -1;
	ll a=0;
	for(int i=b;i<=e;i++){
		a*=10;
		a+=s[i]-'0';
	}
	return a;
}
int main(void){
ll t;
string s;
while(1){
	int time=0;
	cin>>t>>s;
	if(t==0)break;
	ll sum2=0,sum3=0,sum4=0,sum=0;
	ll x=number(0,s.size()-1,s);
//	cout<<x<<endl;
	vector<ll> p2(2,0),p3(3,0),p(4,-1),ans(4,-1);
	if(x<=t){
		cout<<x<<' '<<x<<endl;
		continue;
	}
	ll keta1=1;
	for(int i=1;i<s.size();i++){
		int k=0;
//if(s[i]=='0')continue;
		keta1*=10;
		p[0]=x%keta1;
		if(p[0]>t)break;
		p[1]=x/keta1;
		k=p[0]+p[1];
		if(k<=t&&k>sum){
		
			sum=k;
			ans[0]=p[0];
			ans[1]=p[1];
			ans[2]=-1;
			ans[3]=-1;
			//cout<<keta1<<' '<<sum<<' '<<ans[0]<<' '<<ans[1]<<endl;
			}else{
				ll y=p[1];
				ll keta2=1;
			for(int j=1;j<s.size()-i;j++){
				keta2*=10;
			//if(j==2)cout<<"OK"<<' '<<i<<endl;
				p[2]=y/keta2;
			//	if(s[s.size()-(i+j)]=='0')continue;
				p[1]=y%keta2;
				 if(p[1]>t)break;
				k=p[0]+p[1]+p[2];
				if(sum==k){
					time++;
				}
				if(k<=t&&k>sum){
					time=1;
					sum=k;
					ans[0]=p[0];
					ans[1]=p[1];
					ans[2]=p[2];
					ans[3]=-1;
				//	cout<<keta1<<' '<<keta2<<' '<<y<<' '<<sum<<' '<<ans[0]<<' '<<ans[1]<<endl;
			}else {
					ll z=p[2];
				//	if(z==12&&p[1]==34)cout<<"ok"<<endl;
					ll keta3=1;
					for(int l=1;l<s.size()-i-j;l++){
						keta3*=10;
						p[3]=z/keta3;
					//	if(s[s.size()-(i+j+l)]=='0')continue;
						p[2]=z%keta3;
						if(p[2]>t)break;
						k=p[0]+p[1]+p[2]+p[3];
						if(sum==k)time++;
						if(k<=t&&k>sum){
							time=1;
							sum=k;
							ans[0]=p[0];
							ans[1]=p[1];
							ans[2]=p[2];
							ans[3]=p[3];
				//		cout<<keta1<<' '<<keta2<<' '<<keta3<<' '<<y<<' '<<z<<' '<<sum<<' '<<ans[0]<<' '<<ans[1]<<' '<<ans[2]<<' '<<ans[3]<<endl;
						}
					}
				}
			}

		}


	}
	if(sum==0){
		cout<<"error"<<endl;
	}else if(time>1){
		cout<<"rejected"<<endl;
	}else{
		cout<<sum<<' ';
		for(int i=3;i>=0;i--){
			if(ans[i]!=-1){
				cout<<ans[i];
				if(i!=0)cout<<' ';
			}
			
		}
		cout<<endl;
	}
}
	return 0;
}