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++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
 
const static int tx[] = {0,1,1,1,0,-1,-1,-1};
const static int ty[] = {-1,-1,0,1,1,1,0,-1};

class Node{
private:
  vector<string> constituents;
  int score;
public:
  
  int get_score() const { return score; }
  void print_result() {
    
    printf("%d ",score);
    for(int i=0;i<constituents.size();i++){
      if(i==0){
	cout << constituents[i];
      }
      else{
	cout << " " <<constituents[i];
      }
    }
    printf("\n");
  }
  Node(const string& _c) {
    constituents.push_back(_c);
    score = atoi(_c.c_str());
  }
  
  Node(const string& _c,int _s) : score(_s) {
    constituents.push_back(_c);
  }
  
  void cat(const string& _c,int _s){
    constituents.push_back(_c);
    score = score + _s;
  }
  void erase_last (int last_score){
    score -= last_score;
    constituents.pop_back();
  }
  
};

int main(){
  int target_num;
  string card;
  while(cin >> target_num >> card){
    if(target_num == 0 && card == "0") break;

    //1 ^(1) 2 ^(2) 2 ^(3) 4 ^(4)
    vector<Node> dp[100];
    for(int i=0;i<card.size();i++){
      dp[i].push_back(Node(card.substr(0,i+1)));
      for(int j=0;j<i;j++){
	//0->j j->i
	for(int k=0;k<dp[j].size();k++){
	  int rear = atoi(card.substr(j+1,i-j).c_str());
	  dp[j][k].cat(card.substr(j+1,i-j),rear);
	  dp[i].push_back(dp[j][k]);
	  dp[j][k].erase_last(rear);
	  // printf("%d %d\n",dp[j][k],rear);
	}
      }
    }

    Node res("",-1);
    bool reject = false;
    for(int i=0;i<dp[card.size()-1].size();i++){
      if(target_num >= dp[card.size()-1][i].get_score()){
	if(res.get_score() < dp[card.size()-1][i].get_score()){
	  res = dp[card.size()-1][i];
	  reject = false;
	}
	else if(res.get_score() == dp[card.size()-1][i].get_score()){
	  reject = true;
	}
      }
      // printf("%d\n",dp[card.size()-1][i]);
    }
    
    if(res.get_score() == -1){
      printf("error\n");
    }
    else if(reject){
      printf("rejected\n");
    }
    else{
      res.print_result();
    }
  }
}

C++: #include <iostream>
#include <string>
using namespace std;

int main() {
  int t;
  string S;
  while(cin >> t >> S && t) {
    int N = S.size();
    int max = 0;
    int mc = 0;
    int bit = 0;
    for (int i= (1 << (N-1)); i< (1 << N); i++) {
      int l=0, f=0;
      int count = 0;
      for (int j=0; j<N; j++) {
	if (i & (1 << j)) {
	  l = f;
	  f = j+1;
	  count += stoi(S.substr(l, f-l));
	}
      }
      if (count > max && count <= t) {
	max = count;
	mc = 1;
	bit = i;
      } else if (count == max)  {
	mc++;
      }
    }
    if (mc == 0) {
      cout << "error" << endl;
    } else if (mc == 1) {
      cout << max << " ";
      int l=0, f=0;
      for (int j=0; j<N; j++) {
	if (bit & (1 << j)) {
	  l = f;
	  f = j+1;
	  cout << S.substr(l, f-l); // ここを文字列で出力するのが問題に忠実。
	  // しかし先頭に0がつく紙が答えになることはないので整数でも問題ない。
	  if (f < N) cout << " ";
	  else cout << endl;
	}
      }
    } else {
      cout << "rejected" << endl;      
    }
  }
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<set>
#include<cstdlib>

using namespace std;

int comb(int n, int k, int target, string num, set< vector<string> > &parts){
  int *p;
  bool loop = false;
  int min_diff = target + 1;
  
  p = new int[k];
  for(int i = 0; i < k; i++) p[i] = i;
  
  do{ loop = false;
    
    // TODO
    int sum = 0;
    int st = 0;
    string s;
    vector<string> vp;
    for(int i = 0; i < k; ++i){
      //cout << num.substr(st,p[i]-st+1) << '@';
      s = num.substr(st,p[i]-st+1);
      if(s.length()>0)
	vp.push_back(s);
      st = p[i]+1;
    }
    if(st<n){
      s = num.substr(st,n-st+1);
      if(s.length()>0)vp.push_back(s);
    }

    for(int i = 0; i < (int)vp.size(); ++i){
      sum += atoi(vp[i].c_str());
      //cout << vp[i] << '@';
    }
    //cout << endl;
    //cout << sum << endl;
    if( target - sum >= 0 && min_diff > target - sum ){
      min_diff = target - sum;
      parts.clear();
      parts.insert(vp);
    }else if( target - sum >= 0 && min_diff == target - sum ){
      parts.insert(vp);
    }
    
    for(int pk=k-1;pk>=0;pk--){p[pk]++;if(p[pk]<=n-(k-pk)){for(int j=pk+1;j<k;j++)p[j]=p[j-1]+1;pk++;loop=true;break;}}
  }while(loop);
  delete [] p;
  return min_diff;
}

int solve(int target, string num, set< vector<string> > &parts){
  int len = num.length();
  int min = target + 1;
  for(int part = 1; part <= len; ++part){
    set< vector<string> > tmp_parts;
    int t = comb(len,part,target,num,tmp_parts);
    if( min > t ){
      min = t;
      parts = tmp_parts;
    }else if( min == t ){
      for(set< vector<string> >::iterator it = tmp_parts.begin(); it != tmp_parts.end(); ++it){
	parts.insert( *it );
      }
    }
  }
  return min;
}

int main()
{
  while(true){
    int target;
    string num;
    set< vector<string> > parts;

    cin >> target >> num;
    if( target == 0 && num == "0" ) break;
    int res = solve( target, num, parts );
    if( res == target + 1 ){
      cout << "error" << endl;
    }else if( parts.size() > 1 ){
      cout << "rejected" << endl;
    }else{
      cout << target - res;
      vector<string> p = *(parts.begin());
      for(int i = 0; i < (int)p.size(); ++i){
	cout << ' ' << p[i];
      }
      cout << endl;
    }
  }
  return 0;
}