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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(ll i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_N 100000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

ll dp[MAX_N+1];
ll n, a[MAX_N];

int main(){
  scanf("%lld", &n);
  rep(i, n) scanf("%lld", a+i);
  ll res = 0;
  rep(i, n){
    dp[i] = 1;
    rep(j, i){
      if(a[i] > a[j]) dp[i] = max(dp[j]+1, dp[i]);
    }
    res = max(res, dp[i]);
  }

  printf("%lld\n", res);

  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
vector<int>res;
int ma,cnt;

void dfs(int s,const int t,const string n,vector<int>&v){
	if(s>t)return;
	if(n.empty()){
		if(ma<s)ma=s,cnt=0;
		if(ma==s)res=v,cnt++;
	}else{
		int add=0;
		for(int i=0;i<n.size();i++){
			add=add*10+n[i]-'0';
			v.push_back(add);
			dfs(s+add,t,n.substr(i+1),v);
			v.pop_back();
		}
	}
}

int main(){
	int t;
	string n;
	for(;cin>>t>>n,t;){
		vector<int>v;
		ma=0,cnt=0;
		dfs(0,t,n,v);
		if(cnt>1)puts("rejected");
		else if(!ma)puts("error");
		else{
			printf("%d",ma);
			for(int i=0;i<res.size();i++)printf(" %d",res[i]);
			puts("");
		}
	}
}