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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
using namespace std;

int main(void)
{
	int t;
	string nn;
	for (;;)
	{
		cin >> t >> nn;
		if (!t)break;
		int cal = -1;
		string output = "error";
		for (int f = 0; f < (1 << (nn.size() - 1)); ++f)
		{
			string n = nn;
			int sum = 0;
			vector<string>cash;
			int las = 0, put = 0;
			for (int i = 1; i < nn.size(); ++i)
			{
				++put;
				if (f & (1 << (i - 1)))
				{
					string m = n.substr(0, put);
					for (int k = 0; k < put; ++k)n.erase(n.begin());
					put = 0;
					las = i;
					sum += stoi(m);
					cash.push_back(m);
				}
			}
			if (!n.empty())
			{
				sum += stoi(n);
				cash.push_back(n);
			}
			if (sum <= t)
			{
				if (cal < sum)
				{
					cal = sum;
					output.clear();
					stringstream ss;
					ss << cal;
					ss >> output;
					for (string elm : cash)
					{
						output.append(" "+elm);
					}
				}
				else if (cal == sum)
				{
					output = "rejected";
				}
			}
		}
		cout << output << endl;
	}
	return 0;
}