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 <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <sstream>
#include <map>

using namespace std;

typedef vector<string> vs;

pair<int,vs> solve(int t, int n){
    stringstream ss;
    ss << n;
    string s = ss.str();
    int l=s.size();
    map<int,vector<vs>> ans;

    if(n <= t)ans[n] = {{s}};
    for(int mask=1;mask<1<<(l-1);mask++){
        vs v = {""};
        v[0] += s[0];
        for(int i=0;i<l-1;i++){
            if(mask>>i & 1) v.push_back("");
            v.back() += s[i+1];
        }
        int sum=0;
        for(int i=0;i<v.size();i++){
            sum += atoi(v[i].c_str());
        }
        if(sum <= t){
            ans[sum].push_back(v);
        }
    }


    if(ans.size()==0) return make_pair(-1,vs{"error"});
    if(ans.rbegin()->second.size()>1) return make_pair(-1,vs{"rejected"});
    return make_pair(ans.rbegin()->first,*ans.rbegin()->second.begin());
}

int main(){
    int t,n;
    while(cin>>t>>n && t){
        auto a = solve(t,n);
        if(a.first!=-1){
            cout << a.first << " ";
            for(int i=0;i<(int)a.second.size();i++){
                cout << a.second[i] << (i+1==a.second.size() ? "\n" : " ");
            }
        }else{
            cout << a.second[0] << endl;
        }
    }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int t;
  string num;
  while(cin >> t >> num, t||num!="0") {
    int n = num.size();
    vector<string> ansv;
    int ans = 0;
    bool reject = 0;
    REP(S, 1<<(n-1)) {
      int last = 0;
      vector<string> v;
      REP(i, n-1) {
        if (S>>i & 1) {
          v.push_back(num.substr(last, i-last+1));
          last = i+1;
        }
      }
      v.push_back(num.substr(last));
      int sum = 0;
      FOR(it, v)
        sum += atoi(it->c_str());
      if (sum > ans && sum <= t) {
        ans = sum;
        ansv = v;
        reject = 0;
      } else if (sum == ans) {
        // cout << "hgoidjso" << endl;
        // FOR(it, ansv) {
        //   cout << " " << *it;
        // }
        // cout << endl;
        // FOR(it,v) {
        //   cout << " " << *it;
        // }
        // cout << endl;
        reject = 1;
      }
    }
    if (reject) {
      cout << "rejected" <<endl;
    } else if (ans == 0) {
      cout << "error" << endl;
    } else {
      cout << ans;
      FOR(it, ansv) {
        cout << " " << *it;
      }
      cout << endl;
    }
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <sstream>
#include <map>

using namespace std;

pair<int,vector<string>> solve(int t, int n){
    stringstream ss;
    ss << n;
    string s = ss.str();
    int l=s.size();
    map<int,vector<vector<string>>> ans;

    if(n <= t){
        ans[n] = {{s}};
    }
    for(int mask=1;mask<1<<(l-1);mask++){
        // {
        //     bool ok=true;
        //     for(int i=0;i<l-1;i++){
        //         if(mask>>i & 1){
        //             if(s[i+1]=='0'){
        //                 ok = false;
        //             }
        //         }
        //     }
        //     if(!ok) continue;
        // }
        vector<string> v = {""};
        v[0] += s[0];
        for(int i=0;i<l-1;i++){
            if(mask>>i & 1){
                v.push_back("");
            }
            v.back() += s[i+1];
        }
        int sum=0;
        for(int i=0;i<v.size();i++){
            sum += atoi(v[i].c_str());
        }
        if(sum <= t){
            ans[sum].push_back(v);
        }
    }


    if(ans.size()==0){
        return make_pair(-1,vector<string>{"error"});
    }
    else if(ans.rbegin()->second.size()>1){
        // for(int i=0; i<ans.rbegin()->second.size();i++){
        //     cout << " ";
        //     for(int j=0;j<ans.rbegin()->second[i].size();j++){
        //         cout << ans.rbegin()->second[i][j] << " ";
        //     }
        //     cout << endl;
        // }
        return make_pair(-1,vector<string>{"rejected"});
    }
    return make_pair(ans.rbegin()->first,*ans.rbegin()->second.begin());
}

int main(){
    int t,n;
    while(cin>>t>>n && t){
        auto a = solve(t,n);
        if(a.first!=-1){
            cout << a.first << " ";
            for(int i=0;i<(int)a.second.size();i++){
                cout << a.second[i] << (i+1==a.second.size() ? "\n" : " ");
            }
        }else{
            cout << a.second[0] << endl;
        }
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,ans,l;
string a,s;
multiset<int>pp;
void ch(string t){
  int p=0,x=10,ss=l;
  for(int i=0;i<t.size();i++){
    if(t[i]=='+')p+=ss%x,ss/=x,x=10;
    else x*=10;
  }
    if(ans<=p&&p<=n){
      ans=p,a=t;
      pp.insert(p);
    }
}
void dfs(int d,string t){
  if(d==s.size())ch(t+'+');
  else {
    dfs(d+1,t+'+');
    dfs(d+1,t+'-');
  }
}
int f(string q){
  int o=0;
  for(int i=0;i<q.size();i++)
    o*=10,o+=q[i]-'0';
  return o;
}
string chq(string t){
  reverse(t.begin(),t.end());
  string u;
  u+=t[0];
  for(int i=1;i<t.size();i++){
    if(a[i-1]=='+')u+=' ';
    u+=t[i];
  }
  reverse(u.begin(),u.end());
  return u;
}
int main(){
  while(cin>>n>>s,n){
    l=f(s);
    ans=0;
    dfs(0,"");
    if(ans==0)cout<<"error"<<endl;
    else if(pp.count(ans)>=3)cout<<"rejected"<<endl;
    else{
      cout<<ans<<' ';
      string qq=chq(s);
      cout<<qq<<endl;
    }
  }
}