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;

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) { ll d = euclid(b, a % b, y, x);
        return y -= a/b * x, d; }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1) ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1); return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e) return Mod(1);
        Mod r = *this ^ (e / 2); r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

void proc(int t, string num) {
    map<int, vector<vector<string>>> m;
    for (int i = 0; i < (1 << num.length() - 1); i++) {
        vector<string> parts;
        string buf;
        for (int j = 0; j < num.length(); j++) {
            buf += num[j];
            if (i & (1 << j)) {
                parts.push_back(buf); buf = "";
            }
        }
        if (buf.size()) parts.push_back(buf);
        // if (any_of(all(parts), [](string s) {return s.size() != 1 && s.front() == '0';})) {
        //     continue;
        // }
        int sum = 0;
        for (auto s : parts) sum += stoi(s);
        if (sum > t) continue;
        m[sum].push_back(parts);
    }
    if (m.empty()) {
        cout << "error" << endl;
    } else if (m.rbegin()->second.size() > 1) {
        cout << "rejected" << endl;
    } else {
        auto p = m.rbegin()->second.front();
        cout << m.rbegin()->first << " ";
        for (int i = 0; i < p.size(); i++) {
            cout << p[i] << " \n"[i + 1 == p.size()];
        }
    }
}

int main() {
    for (;;) {
        int t, num; cin >> t >> num;
        if (!t && !num) break;

        proc(t, to_string(num));
    }
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

int main() {
    int x; string b;
    while(cin >> x >> b, x){
        int n = b.size();
        vector<string> ans2;
        int ans1 = -1, c = 0;
        for (int i = 0; i < (1<<(n-1)); ++i) {
            int mask = i^(1<<(n-1));
            string s;
            int sum = 0;
            vector<string> cut;
            for (int j = 0; j < n; ++j) {
                s += b[j];
                if(mask & (1<<j)){
                    sum += stoi(s);
                    cut.emplace_back(s);
                    s = "";
                }
            }
            if(sum <= x){
                if(sum == ans1) c++;
                if(sum > ans1){
                    c = 1;
                    ans1 = sum;
                    ans2 = cut;
                }
            }
        }
        if(c == 0){
            puts("error");
        }else if(c >= 2){
            puts("rejected");
        }else {
            cout << ans1;
            for (auto &&i : ans2){
                cout << " " << i;
            }
            puts("");
        }
    }
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(vector<int>::iterator itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define MP make_pair

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

int t, max_num, f = 0;
vector<int> ans;

int get_nextnum(int x){
  char s[10];
  sprintf(s, "%d", x);
  return s[0] + '0';
}

int calc_digit(int x){
  int res = 0;
  while(x != 0){
    x /= 10;
    res++;
  }
  return res;
}

void solve(vector<int> v, int digit, int n, int num){
  if(n == digit){
    int sum = 0;
    v.pb(num);
    REPI(itr,v){
      sum += *itr;
    }
    if(t >= sum && sum == max_num){
      f = 1;
    }else if(t >= sum && sum > max_num){
      max_num = sum;
      f = 0;
      ans.clear();
      copy(v.begin(), v.end(), back_inserter(ans));
    }
    return;
  }
  
  solve(v, digit, n + 1, num);
  if(get_nextnum(num) != 0){
    int tmp = 1;
    REP(i,digit - n){
      tmp *= 10;
    }
    v.pb(num / tmp);
    solve(v, digit, n + 1, num % tmp);
  }
}

int main(){

  while(true){
    int num;
    scanf("%d %d", &t, &num);
    
    if(t == 0 && num == 0)  break;
    if(t == num){
      printf("%d %d\n", t, num);
      continue;
    }
    int digit = calc_digit(num);
    max_num = -1, f = 0;
    vector<int> v;
    solve(v, digit, 1, num);
    if(max_num == -1){
      printf("error\n");
    }else if(f == 1){
      printf("reject\n");
    }else{
      printf("%d ", max_num);
      REPI(itr, ans){
        printf("%d", *itr);
        vector<int>::iterator itrt = itr;
        itrt++;
        if(itrt != ans.end()){
          printf(" ");
        }
      }
      printf("\n");
    }
  }
  return 0;
}