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;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n; string s;
    while (cin >> n >> s, n || s != "0") {
        ll m = linf;
        vector<ll> ans;
        bool rejected = false;
        try {
            set<vector<ll>> used;
            rep(a, 7) rep(b, 7) rep(c, 7) rep(d, 7) rep(e, 7) rep(f, 7) {
                if (a + b + c + d + e + f != s.size()) continue;
                vector<ll> v;
                if (a) v.pb(a);
                if (b) v.pb(b);
                if (c) v.pb(c);
                if (d) v.pb(d);
                if (e) v.pb(e);
                if (f) v.pb(f);
                if (used.count(v) > 0) continue;
                used.insert(v);
                ll cnt = 0;
                vector<ll> vals;
                bool flag = true;
                rep(i, v.size()) {
                    string str = s.substr(cnt, v[i]);
                    if (str.size() > 1 && str[0] == '0') {
                        // flag = false;
                        // break;
                    }
                    vals.pb(stoll(str));
                    cnt += v[i];
                }
                if (!flag) continue;
                ll sum = 0;
                rep(i, vals.size()) sum += vals[i];
                if (sum > n) continue;
                if (n - sum == m) {
                    rejected = true;
                }
                else if (n - sum < m) {
                    rejected = false;
                    m = n - sum;
                    ans = vals;
                }
            }
            if (rejected) throw string("rejected");
            if (m == linf) throw string("error");
            cout << n - m << " " << ans << endl;
        }
        catch (string err) {
            cout << err << endl;
        }
    }
}

C++: #include<iostream>
#include<string>
#include<stack>
#include<list>
#include<cstdio>

using namespace std;

class Ans{
  int minimam;
  int target;
  list<int> st;
  stack<string> ans;
  const char *str;
  void prl(){
    for(list<int>::iterator it=st.begin();it!=st.end();it++){
      printf("%d ",*it);
    }
    printf("\n");
  }

  void check(int n){
    if(n<0){
      return;
    }else if(n<=minimam){
      char buff[10];
      string s;
      sprintf(buff,"%d ",target-n);
      s=buff;
      for(list<int>::iterator it=st.begin();it!=st.end();it++){
        sprintf(buff,"%d ",*it);
        s+=buff;
      }
      if(n<minimam){
        ans=stack<string>();
      }
      ans.push(s);
      minimam=n;
    }
  }
  void answer(const char *str,int now,int dist){
    if(*str == '\0' && now!=-1) {
      st.push_back(now);
      check(dist-now);
      st.pop_back();
      return;
    }
    if(now!=-1){
      st.push_back(now);
      //cout << now << endl;
      //prl();
      answer(str,-1,dist-now);
      st.pop_back();
    }
    {
      if(now==-1) now=0;
      now=now*10+(*str - '0');
      if(now > dist) return;
      answer(str+1,now,dist);
    }
  }
public:
  Ans(int target,const char *str){
    this->target = target;
    this->str = str;
    this->minimam = 0x7FFFFFFF;
    answer(this->str,-1,this->target);
  }
  void print(){
    if(ans.size()==1){
      cout << ans.top() << endl;
    }else if(ans.size()>1){
      cout << "rejected" << endl;
    }else{
      cout << "error" << endl;
    }
  }
};

int main(){
  int n;
  string m;
  while(cin >> n >> m , !(n==0&&m=="0")){
    Ans a(n,m.c_str());
    //cout << endl;
    a.print();
    //cout << endl;
  }
  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);
      }
      printf("\n");
    }
  }
  return 0;
}