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 <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <stack>
   
using namespace std;

int main()
{
    int t;
    string n;
    while(1){
        cin >> t >> n;
        if(t == 0 && n == "0") break;
        int num[7][7] = {};
        for(int i = 0; i < 7; i++){
            for(int j = 0; j < 7; j++){
                num[i][j] = -1;
            }
        }
        int cnt = 6;
        int l = n.length();
        for(int i = 1; i < l + 1; i++){
            num[i][1] = n[i - 1] - '0';
        }
        for(int i = 1; i < l + 1; i++){
            // cout << num[i][1] << endl;
            // if(num[i][1] == 0) continue;
            for(int j = 2; i + j <=  l + 1; j++){
                // if(num[i][j - 1] == -1) continue;
                num[i][j] = num[i][j - 1] * 10 + num[i + j - 1][1];
                // cout << num[i][j] << endl;
            }
        }
        bool isrejected = false;
        bool iserror = true;
        int ans = 1000000;
        vector<int> ansv;
        // cout << ans << endl;
        for(int i = 0; i < (1 << (l - 1)); i++){
            int result = 0;
            int start = 1;
            int cnt = 1;
            vector<int> resultv;
            bool isend = false;
            while(start + cnt <= l + 1){
                while(start + cnt <= l && (i & (1 << (start + cnt - 2))) == 0) cnt++;
                // cout << num[start][cnt] << endl;
                // if(num[start][cnt] == -1){
                //     isend = true;
                //     break;
                // }
                result += num[start][cnt];
                resultv.push_back(num[start][cnt]);
                start = start + cnt;
                cnt = 1;
            }
            // if(isend) continue;
            // cout << result << endl;
            // cout << endl;
            if(result <= t){
                if(abs(ans - t) == abs(result - t)){
                    isrejected = true;
                }
                else if(abs(ans - t) > abs(result - t)){
                    ans = result;
                    ansv = resultv;
                    isrejected = false;
                    iserror = false;
                }
            }
        }
        if(iserror){
            cout << "error" << endl;
        } else if(isrejected){
            cout << "rejected" << endl;
        } else {
            cout << ans;
            for(int i = 0; i < ansv.size(); i++){
                cout << " " << ansv[i];
            } 
            cout << endl;
        }
    }
    return 0;
}

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

vector<int> solve(int t, string num, vector<int> ret, int i, int k){
  if(i == -1) return ret;
  bool f = false, g = false;
  vector<int> r1, r2;
  if(ret[0] != -1 && ret[0] + k*10*(num[i]-'0') <= t){
    ret[ret.size()-1] += k*10*(num[i]-'0');
    ret[0] += k*10*(num[i]-'0');
    r1 = solve(t,num,ret,i-1,k*10);
    ret[ret.size()-1] -= k*10*(num[i]-'0');
    ret[0] -= k*10*(num[i]-'0');
    f = (r1[0] != -1);
  }
  if(ret[0] != -1 && ret[0] + (num[i]-'0') <= t){
    ret.push_back(num[i]-'0');
    ret[0] += (num[i]-'0');
    r2 = solve(t,num,ret,i-1,1);
    ret.pop_back();
    ret[0] -= (num[i]-'0');
    g = (r2[0] != -1);
  }
  if(f && g){
    if(r1[0] == r2[0]){
      vector<int> r(2,-1);
      r[0] = r1[0];
      return (r);
    }else{
      return max(r1,r2);
    }
  }
  if(f) return r1;
  if(g) return r2;
  vector<int> r(1,-1);
  return r;
}

int main(){
  int t;
  string num;
  while(cin >> t >> num, t){
    int n = num.length();
    vector<int> init(1,num[n-1]-'0');
    init.push_back(num[n-1]-'0');
    vector<int> ans = solve(t,num,init,n-2,1);
    if(ans[0] == -1) cout << "error" << endl;
    else if(ans[1] == -1) cout << "rejected" << endl;
    else{
      cout << ans[0];
      for(int i = ans.size()-1; i > 0; --i)
        cout << " " << ans[i];
      cout << endl;
    }
  }
  return 0;
}

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

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

int main(){
  while(1){
    int n,m;
    cin >> n >> m;
    if(n==0) return 0;
    vector<int> v;
    int tmp=m;
    while(tmp) v.push_back(tmp%10),tmp/=10;
    reverse(all(v));
    m=v.size();
    int count=0,aa=-1;
    vector<int> ans;
    rep(i,1<<(m-1)){
      vector<int> x;
      x.push_back(v[0]);
      rep(j,m-1){
        if(i&(1<<j)){
          x.push_back(v[j+1]);
        }else{
          x[x.size()-1]=x[x.size()-1]*10+v[j+1];
        }
      }
      int sum=0;
      rep(j,x.size()) sum+=x[j];
      if(sum<=n){
        if(aa<sum){
          ans=x;
          aa=sum;
          count=1;
        }else if(sum==aa){
          count++;
        }
      }
    }
    if(count==0){
      cout << "error" << endl;
    }else if(count>1){
      cout << "rejected" << endl;
    }else{
      int aaa = ans.size();
      cout << aa << " ";
      rep(i,aaa-1){
        cout << ans[i] << " ";
      }
      cout << ans[aaa-1] << endl;
    }
  }
  return 0;
}