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 <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int p10(int n) {
    int ret = 1;
    for (int i = 0; i < n; i++) {
        ret *= 10;
    }
    return ret;
}

int main(void) {
    int target, num;
    while (cin >> target >> num) {
        if (target == 0 && num == 0) break;
        int keta = 0, tmp = num;
        while (tmp) {
            keta++;
            tmp /= 10;
        }
        int dest = -INF;
        for (int digit = 0; digit < (1<<(keta-1)); digit++) {
//            cout << digit << endl;
            int sum = 0, old = -1;
            for (int i = 0; i < digit; i++) {
                if (((digit >> i) & 1) == 1) {
                    sum += (num % p10(i+1)) / p10(old+1);
                    old = i;
//                    printf("+%d\n", (num % p10(i+1) / p10(old+1)));
                }
            }
            sum += (num % p10(keta)) / p10(old+1);
//            printf("+%d\n", (num % p10(keta) / p10(old+1)));
            if (sum <= target) {
                dest = max(sum, dest);
            }
        }
        if (dest == -INF) cout << "error" << endl;
        
        else {
            int cnt = 0;
            int index = 0;
            bool flag = true;
            for (int digit = 0; digit < (1<<(keta-1)); digit++) {
                int sum = 0, old = -1;
                for (int i = 0; i < keta; i++) {
                    if (((digit >> i) & 1) == 1) {
                        sum += (num % p10(i+1)) / p10(old+1);
                        old = i;
                    }
                }
                sum += (num % p10(keta)) / p10(old+1);
                if (sum == dest) {
                    cnt++;
                    if (cnt > 1) {
                        cout << "rejected" << endl;
                        flag = false;
                        break;
                    }
                    index = digit;
                }
            }
            if (flag) {
                vector<int> ans;
                cout << dest;
                int old = -1;
                for (int i = 0; i < keta; i++) {
                    if (((index >> i) & 1) == 1) {
                        ans.push_back((num % p10(i+1)) / p10(old+1));
                        old = i;
                    }
                }
                ans.push_back((num % p10(keta)) / p10(old+1));
                reverse(ans.begin(), ans.end());
                for (int i = 0; i < ans.size(); i++) {
                    cout << " " << ans[i];
                }
                cout << endl;
            }
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<(n);++i)
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RREP(i, n) for(int i=(int)(n)-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=(int)(b)-1;i>=(a);--i)
#define ALL(a) a.begin(),a.end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2>
bool chmax(T1 &a, const T2 &b) {
    if (a < b) { a = b; return true; }
    return false;
}
template <typename T1, typename T2>
bool chmin(T1 &a, const T2 &b) {
    if (a > b) { a = b; return true; }
    return false;
}
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
    os << "{";
    int a = 0;
    for (auto &tp : mp) {
        if (a) os << ", "; a = 1;
        os << tp;
    }
    return os << "}";
}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << p.first << ":" << p.second;
    return os;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    const int di[] = {0, 1, 0, -1, 1, 1, -1, -1};
    const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1};

    for (;;) {
        int T, N; cin >> T >> N;
        if (T == 0 and N == 0) break;

        int k = 0;
        for (int n = N; n > 0; n /= 10) ++k;
        int ma = 0;
        vvi cands;
        REP(s, 1<<k-1) {
            int prev = 0;
            vi pieces;
            REP(i, k-1) if (s >> i & 1) {
                pieces.push_back(stoi(to_string(N).substr(prev, i - prev + 1)));
                prev = i+1;
            }
            pieces.push_back(stoi(to_string(N).substr(prev, k)));
            int sum = accumulate(ALL(pieces), 0);
            if (sum > T) continue;
            if (chmax(ma, sum)) {
                cands.clear();
                cands.push_back(pieces);
            } else if (ma == sum) {
                cands.push_back(pieces);
            }
        }

        if (ma == 0) {
            cout << "error" << endl;
        } else if (cands.size() > 1) {
            cout << "rejected" << endl;
        } else {
            cout << ma;
            for (int a : cands[0]) {
                cout << " " << a;
            }
            cout << endl;
        }

    }

    return 0;

}