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 <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
//#define int ll

signed main(void)
{
	while(1) {
		int n;
		string m;
		cin >> n >> m;
		if(!n && m == "0") break;

		bool rej = false, err = true;
		int ma = -1, ans[10] = {0}, sz = 0;
		if(to_string(n) == m) {
			ma = n;
			err = false;
			sz = 2;
			ans[0] = ans[1] = n;
		}
		FOR(i, 1, 1<<(m.size()-1)) {
			string v[10] = {""};
			v[0] = m[0];
			int idx = 0;
			REP(j, m.size()-1) {
				if(i>>j&1) idx++;
				v[idx] += m[j+1];
			}
			int sum = 0;
			REP(j, idx+1) {
				//cout << v[j] << " ";
				sum += stoi(v[j]);
			}
			//cout << "sum:" << sum << endl;
			if(sum <= n) {
				err = false;
				if(sum > ma) {
					ma = sum;
					rej = false;
					//cout << "a" << endl;
					ans[0] = sum;
					REP(j, idx+1) ans[j+1] = stoi(v[j]);
					sz = idx+2;
				} else if(sum == ma) {
					rej = true;
				}
			}
		}
		if(err) {
			cout << "error" << endl;
		} else if(rej) {
			cout << "rejected" << endl;
		} else {
			REP(i, sz) {
				cout << ans[i];
				if(i != sz-1) cout << " ";
			}
			cout << endl;
		}
	}

	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){
  while(1){
    int t;
    string num;
    cin >> t >> num;
    if(t==0 && num=="0") break;

    int now=0;
    int same_ct=0;
    int small=1000000;
    vector<int> ans;

    //???????????????????????°????????°
    int len = num.size();
    //?????????????????????????????????????????§bitloop
    for(int i=0; i<(1<<(len-1)); ++i){
      vector<int> s;

      int st=0;
      //printf(" %2d:", i);
      for(int j=0; j<len-1; ++j){
        if(i>>j & 1){
          s.push_back(atoi(num.substr(st,j+1-st).c_str()));
          st=j+1;
        }
      }
      s.push_back(atoi(num.substr(st,len+1-st).c_str()));

      int tmp=0;
      for(int j=0; j<s.size(); ++j) tmp+=s[j];
      //printf("%5d\n", tmp);

      small = min(small, tmp);

      if(t<tmp) continue;
      if(now<tmp){
        ans.resize(s.size());
        for(int j=0; j<s.size(); ++j) ans[j]=s[j];
        now=tmp;
        same_ct=1;
      }
      else if(now==tmp) same_ct++;
    }

    //output
    if(small>t) printf("error\n");
    else if(same_ct>1) printf("rejected\n");
    else{
      printf("%d", now);
      for(int i=0; i<ans.size(); ++i) printf(" %d", ans[i]);
      printf("\n");
    }
  }
}

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

typedef vector<int> vec;
typedef vector<vec> mat;

const int INF = 1 << 28;
int sum;
string num;

int main() {
    while (cin >> sum >> num, sum || num != "0") {
        map<int,vec> m;
        int target = -1;
        for (int i = 0; i < 1 << (num.size() - 1); i++) {
            int tmp = 0;
            int pre = 0;
            for (int j = 0; j < num.size() - 1; j++) {
                if (i & (1 << j)) {
                    string s = string(num.begin() + pre, num.begin() + j + 1);
                    // if (s[0] == '0' && s.size() > 1) continue;
                    stringstream ss; ss << s;
                    int n; ss >> n;
                    tmp += n;
                    pre = j + 1;
                }
            }

            string s = string(num.begin() + pre, num.end());
            stringstream ss; ss << s;
            int n; ss >> n;
            tmp += n;

            if (tmp <= sum) {
                m[tmp].push_back(i);
                target = max(target, tmp);
            }
        }

        if (m.size() == 0) {
            cout << "error" << endl;
        } else if (m[target].size() == 1) {
            cout << target << " ";
            int tmp = m[target][0];
            for (int i = 0; i < num.size(); i++) {
                cout << num[i];
                if (tmp & (1 << i)) cout << " ";
            }
            cout << endl;
        } else {
            cout << "rejected" << endl;
        }
    }
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        int n;
        string s;
        cin >> n >> s;
        if(n == 0)
            return 0;

        int len = s.size();
        int ret = INT_MIN;
        int cut;
        bool rejected;
        for(int i=0; i<(1<<(len-1)); ++i){
            bitset<5> bs(i);
            int a = s[0] - '0';
            int b = 0;
            for(int j=0; j<len-1; ++j){
                if(bs[j]){
                    b += a;
                    a = 0;
                }
                a *= 10;
                a += s[j+1] - '0';
            }
            b += a;

            if(b <= n){
                if(b > ret){
                    ret = b;
                    cut = i;
                    rejected = false;
                }else if(b == ret){
                    rejected = true;
                }
            }
        }

        if(ret == INT_MIN){
            cout << "error" << endl;
        }else if(rejected){
            cout << "rejected" << endl;
        }else{
            cout << ret << ' ';
            bitset<5> bs(cut);
            for(int i=0; i<len-1; ++i){
                cout << s[i];
                if(bs[i])
                    cout << ' ';
            }
            cout << s[len-1] << endl;
        }
    }
}