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

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

  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 <algorithm>
#include <numeric>
#include <vector>
#include <cassert>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <fstream>
#include <cmath>
using namespace std;

#define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for(ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n) & 1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;



int best;
int best_count;
vector<int> res;


void dfs(string n, int sum, int target, vector<int> v){
  if(sum > target) return;
  if(n == ""){
    if(sum > best){
      best = sum;
      best_count = 1;
      res = v;
    }else if(sum == best){
      best_count++;
    }
    return;
  }
  
  REP(i, n.size()){
    string n1 = n.substr(0, i + 1);
    string n2 = n.substr(i + 1);
    
    vector<int> v2 = v;
    v2.push_back(atoi(n1.c_str()));
    dfs(n2, sum + atoi(n1.c_str()), target, v2);
  }
}
int main(){
  string sheet;
  int target;
  while(cin >> target >> sheet && (sheet != "0" || target > 0)){
    best = -1;
    best_count = 0;
    dfs(sheet, 0, target, vector<int>());
    if(best == -1){
      cout << "error" << endl;
    }else if(best_count > 1){
      cout << "rejected" << endl;
    }else{
      cout << best << " " ;
      REP(i, res.size()) cout << res[i] << (i + 1 == (int)res.size() ? '\n' : ' ');
    }
  }
  return 0;
}