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>

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;

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

void solve(string s, int num){

  int dig = make(s);
  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,2);
    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 <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define R(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

int64 res = -1;
bool out = 0;
int64 n;
vector<int64> rv, now;
void dfs(const string& s, int64 d, int64 sum) {
    if (d == s.size()) {
        if (sum <= n) {
            if (res == -1) {
                rv = now; res = sum;
            } else if (res == sum) {
                out = 1;
            } else if (res < sum) {
                rv = now;
                res = sum;
                out = 0;
            }
        }
    }
    int64 nxt = 0;
    FOR(i, d, s.size()) {
        nxt = nxt * 10 + s[i]-'0';
        now.push_back(nxt);
        dfs(s, i+1, sum+nxt);
        now.pop_back();
    }
}

int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);

    string s;
    while (cin >> n && cin >> s && n != 0 && s != "0") {
        rv = vector<int64>();
        now = vector<int64>();
        res = -1;
        out = 0;
        dfs(s, 0, 0);
        if (out) {
            cout << "rejected" << endl;
        } else if (res == -1) {
            cout << "error" << endl;
        } else {
            cout << res << " ";
            REP(i, rv.size()) {
                cout << rv[i] << (i == rv.size()-1 ? "" : " ");
            }
            cout << endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

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

using namespace std;

int dig(int n) {
  if(n<10)return 1;
  else return dig(n/10)+1;
}

int main() {
  while(1){
    int t,n;
    cin>>t>>n;
    if(!n)break;
    t=min(t,n);
    int s=dig(n);
    string ns=to_string(n);
    vector<vector<int>> dp(s+1,vector<int>(n+1));
    dp[0][0] = 1;
    REP(i,s+1){
      REP(j,i){
        auto sub=ns.substr(j,i-j);
        int m=stoi(sub);
        REP(k,n-m+1){
          if (dp[i][k+m] == 0 && dp[j][k] > 0)
            dp[i][k+m] = j+1;
          else if ((dp[i][k+m] != 0 && dp[j][k] != 0) || dp[j][k] < 0)
            dp[i][k+m] = -1;
        }
      }
    }
    bool ok=false;
    for (int u=t;u>0;--u) {
      if (dp[s][u] > 0) {
        vector<int> res;
        int i=s;
        int j=u;
        do {
          int oldi=i;
          i=dp[i][j]-1;
          auto sub=ns.substr(i,oldi-i);
          res.push_back(stoi(sub));
          j-=stoi(sub);
        } while(i>0);
        reverse(begin(res),end(res));
        cout<<u;
        for(int v:res) cout<<' '<<v;
        cout<<endl;
        ok = true;
        break;
      } else if (dp[s][u] < 0) {
        cout<<"rejected"<<endl;
        ok = true;
        break;
      }
    }
    if (!ok)
      cout<<"error"<<endl;
  }
  return 0;
}

C++: import java.util.*;
class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		while(true) {
			int t = sc.nextInt();
			int n = sc.nextInt();
			
			if(t == 0 && n == 0) break;
			int len = String.valueOf(n).length();
			int ans = -1;
			
			for(int i = 0; i < 1 << len; i++) {
				int sum = 0;
				String tmp = String.valueOf(i);
				int b = 0;
				for(int j = 0; j < tmp.length(); j++) {
					if(tmp.charAt(j) == '1') {
						sum += Integer.parseInt(tmp.subString(b,j));
						b = j;
					}
				}
				if(sum > ans && sum <= t) {
					ans = sum;
				}
			}
			System.out.println(ans);