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;
 
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign

vi num, ans;
int n, ans_max;
bool rejected;
string input;

void solve(string data);

signed main()
{
    while (cin >> n >> input, n) {
        ans_max = -1;
        solve(input);
        if (rejected) {
            cout << "rejected" << endl;
        } else if (ans_max == -1 ) {
            cout << "error" << endl;
        } else {
            cout << ans_max;
            for (int i : ans) {
                cout << " " << i;
            }
            cout << endl;
        }
    }
    return 0;
}

void solve(string data)
{
    int a = 0;
    if (!data.size()) {
        int tmp = 0;
        for (int i : num) {
            tmp += i;
        }
        if (tmp > ans_max && tmp <= n) {
            rejected = false;
            ans_max = tmp;
            ans = num;
        } else if (tmp == ans_max) {
            rejected = true;
        }
    }
    if (data[0] == '0') {
        num.eb(0);
        solve(&data[1]);
        num.pop_back();
    } else {
        rep(i, data.length()) {
            a *= 10;
            a += data[i] - '0';
            num.eb(a);
            solve(&data[i+1]);
            num.pop_back();
        }
    }
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define pi acos(-1.0)
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

int main(){
	int t;
	string s;
	while (cin >> t >> s && t){
		int sum = 0, n = s.length();
		bool rej = 0;
		VI ans;
		REP(i,1<<(n-1)){
			VI res;
			int x = 0;
			REP(j,n){
				x = 10*x + s[j] - '0';
				if ((i>>j) & 1){
					res.push_back(x);
					x = 0;
				}
			}
			res.push_back(x);
			int p = 0;
			REP(j,res.size()) p += res[j];
			if (p > t) continue;
			if (p > sum){
				sum = p;
				ans = res;
				rej = 0;
			}else if (p == sum){
				rej = 1;
			}
		}
		if (rej) {
			cout << "rejected" << endl;
		}else if (sum == 0){
			cout << "error" << endl;
		}else{
			cout << sum;
			REP(i,ans.size()) cout << " " << ans[i];
			cout << endl;
		}
	}
    return 0;
}

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<vector>
#include<string>
#include<map>
#include<algorithm>
#include<sstream>
using namespace std;

typedef pair<int,int> P;

int stoi(string s){
  stringstream ss(s);
  int res; ss>>res;
  return res;
}

int main(){
  int n,ans,mul;
  vector<int> list;
  string m;

  while(cin >> n >> m,n||(m!="0")){
    int l = m.size()-1;
    ans = -1; mul = 0;
    for(int i=0;i<(1<<l);i++){
      vector<P> v; int prv = 0;
      for(int j=0;j<l;j++){
	if((i>>j)&1){
	  v.push_back(P(prv,j)); prv = j+1;
	}
      }
      v.push_back(P(prv,l));
      
      int sum = 0;
      for(int j=0;j<v.size();j++)
	sum += stoi(m.substr(v[j].first,v[j].second-v[j].first+1));
      
      if(sum>n)continue;
      if(sum == ans)mul++;
      else if(sum > ans){
	ans = sum; mul = 0;
	list.clear();
	for(int j=0;j<v.size();j++)
	  list.push_back(stoi(m.substr(v[j].first,v[j].second-v[j].first+1)));
      }
    }
    if(ans<0)cout << "error\n";
    else if(mul)cout << "rejected\n";
    else{
      cout << ans;
      for(int i=0;i<list.size();i++)cout << " " << list[i];
      cout << endl;
    }
  }
}