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<iostream>
#include<string>
#include<vector>
#include<cstdlib>
using namespace std;
#define INFTY (1<<21)
int target;
string source;
vector<vector<string> > ans;
int diff;

void update(vector<int> c ){
    vector<string> s;
    for ( int i = 0; i < c.size()-1; i++ ){
	s.push_back(source.substr(c[i], c[i+1]-c[i]));
    }
    int sum = 0;
    for ( int i = 0; i < s.size(); i++ ) sum += atoi(s[i].c_str());

    if ( sum > target ) return;

    if ( target - sum == diff ){
	ans.push_back(s);
    } else if ( target - sum < diff ){
	diff = target - sum;
	ans.clear();
	ans.push_back(s);
    }
}

void rec(int pos, vector<int> c){
    if ( pos == source.size() ){
	c.push_back(pos);
	update(c);
	return;
    }
    rec(pos+1, c);
    c.push_back(pos);
    rec(pos+1, c);
    c.pop_back();
}

void compute(){
    vector<int> c;
    c.push_back(0);
    diff = INFTY;
    ans.clear();
    rec(1, c);

    if ( diff == INFTY ) cout << "error" << endl;
    else if ( ans.size() > 1 ) cout << "rejected" << endl;    
    else { 
	cout << target - diff;
	for ( int i = 0; i < ans[0].size(); i++ ) cout << " " << ans[0][i];
	cout << endl;
    }
}

main(){
    while( cin >> target >> source){
	if ( target == 0 && source == "0" ) break;
	compute();
    }
}

C++: #include<iostream>
#include<sstream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
  while (true) {
    int t;
    string n;
    cin >> t >> n;
    if (t == 0 && n == "0") break;
    int res = 0, b;
    rep (i, 1 << (n.size() - 1)) {
      int p = 0, r = 0;
      rep (j, n.size() - 1) {
	if (i & 1 << j) {
	  stringstream ss;
	  ss << n.substr(p, j - p + 1);
	  int a;
	  ss >> a;
	  r += a;
	  p = j + 1;
	}
      }
      stringstream ss;
      ss << n.substr(p);
      int a;
      ss >> a;
      r += a;
      if (r <= t && res < r) {
	res = r;
	b = i;
      } else if (res == r) {
	b = -1;
      }
    }
    if (res == 0) cout << "error" << endl;
    else if (b == -1) cout << "rejected" << endl;
    else {
      cout << res << " " << n[0];
      rep (i, n.size() - 1) {
	if (b & 1 << i) cout << " ";
	cout << n[i + 1];
      }
      cout << endl;
    }
  }
  return 0;
}