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<vector>
#include<algorithm>

using namespace std;
typedef pair<int,int> P;
int t, in, lim;
P ans;
string ans_s;

string change(string s, int n){
  int a = 0;
  string out = "";

  for(int i = 0; i < s.length(); i++){
    out += s[i];
    if(s[i] >= '0' && s[i] <= '9') a++;
    if(a == n) out += " ";
  }
  return out;
}


int make(string s){
  s += " ";
  int out = 0;
  int now = 0;
  string tmp = "";

  for(int i = 0; i < s.length(); i++){
    if(s[i] < '0' || s[i] > '9'){
       if(tmp.length() > 1 && tmp[0] == '0') return -1;
      out += now;
      now = 0;
      tmp = "";
    }else{
      now *= 10;
      tmp += s[i];
      now += s[i]-'0';
    }
  }
  return out;
}

void solve(string s, int num){
 
  int dig = make(s);
  // cout << s << "  " << dig << endl;
  if(dig != -1){
    if(dig <= t && dig >= ans.first){ 
      if(dig == ans.first && ans_s != s) ans.second++;
      else{
	ans.first = dig;
	ans.second = 1;
	ans_s = s;
      }
    }
  }
  if(num < lim){
    //do not cut 
    solve(s,num+1);
    //cut
    s = change(s,num);
    solve(s,num+1);
  }
}

bool isError(string s){
  int n = 0;
  for(int i = 0; i < s.length(); i++)
    n+=s[i]-'0';

  return t<n;
}

string tos(int num){
  string out = "";
  while(num){
    out += num%10+'0';
    num/=10;
  }
  reverse(out.begin(),out.end());
  return out;
}


int main(){
  while(cin >> t >> in && in){
    ans = P(-1,0);
    string s = tos(in);
    if(isError(s)) cout << "error" << endl;
    else{
      lim = s.length();
      solve(s,1);
      if(ans.second > 1) cout << "rejected" << endl;
      else cout << ans.first << " " << ans_s << endl;
    }
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
using namespace std;
 
#define INF (1<<29)
 
int toInt(string s){
  istringstream iss(s);
  int a;
  iss >> a;
  return a;
}
 
int main(){
  int n;
  string s;
  while(cin >> n >> s){
    if(n == 0 && s == "0") break;
    int ans = -1;
    vector<string> ans2;
    map<int, int> is_found;
     
    for(int i = 0 ; i < (1<<(s.size()-1)) ; i++){
      vector<string> tmp;
      int pos = 0, point = 0;
      for(int j = 0 ; j < s.size() ; j++){
	if(j == s.size()-1){
	  string t = s.substr(pos, j-pos+1);
	  tmp.push_back(t);
	  pos = j+1;
	  point += toInt(t);
	}
	
	else if((1<<j) & i){
	  string t = s.substr(pos, j-pos+1);
	  tmp.push_back(t);
	  pos = j+1;
	  point += toInt(t);
	}
      }
      
      is_found[point]++;
      if(point <= n && n-point < n-ans){
	ans = point;
	ans2 = tmp;
      }
    }
    
    bool f = true;
    if(is_found[ans] > 1) f = false;
    
    if(ans == -1) cout << "error" << endl;
    else if(!f){
      cout << "rejected" << endl;
    }
    else{
      cout << ans << ' ';
      for(int i = 0 ; i < ans2.size() ; i++){
	cout << ans2[i];
	if(i != ans2.size()-1) cout << ' ';
	else cout << endl;
      }
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	string num;
	while (cin >> t >> num, t || num != "0") {
		int n = num.size();
		int ma = -1, cnt, res;
		for (int i = 0; i < (1 << (n - 1)); i++) {
			int val = 0, tmp = num[0] - '0';
			for (int j = 1; j < n; j++) {
				if (i & (1 << (j - 1))) {
					val += tmp;
					tmp = 0;
				}
				tmp *= 10;
				tmp += num[j] - '0';
			}
			val += tmp;
			if (val <= t) {
				if (val > ma) {
					ma = val;
					res = i;
					cnt = 0;
				}
				else if (val == ma) {
					cnt++;
				}
			}
		}
		if (ma == -1) {
			cout << "error" << endl;
		}
		else if (cnt) {
			cout << "rejected" << endl;
		}
		else {
			cout << ma << ' ';
			for (int i = 0; i < n; i++) {
				cout << num[i];
				if (res & (1 << i)) {
					cout << ' ';
				}
			}
			cout << endl;
		}
	}
	return 0;
}