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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <sstream>
#include <deque>

#define REP(i, n) for ( int i = 0; i < n; i++ )
#define NIL -1

using namespace std;

int N, maxVal = -1;
vector<vector<int> > ans;

int getFigure(int num) {
    int ret = 0;
    while(num > 0) {
        num /= 10;
        ret++;
    }
    return ret;
}

void merge(vector<int> &nums, int idx) {
    int nonZero = 0,
        prevIdx = 0;
    REP(i, nums.size()) {
        if(nums[i] == NIL) continue;
        nonZero++;

        if(nonZero == idx + 2) {
            int step = getFigure(nums[i]);
            REP(j, step) nums[prevIdx] *= 10;
            nums[prevIdx] += nums[i];
            nums[i] = NIL;
            break;
        }
        prevIdx = i;
    }
}

void toEmpty(vector<vector<int> > &vec) {
    REP(i, vec.size()) vec.clear();
}

int sum(vector<int> &nums) {
    int ret = 0;
    REP(i, nums.size()) if(nums[i]!=NIL) ret += nums[i];
    return ret;
}

void dump(vector<int> &nums) {
    REP(i, nums.size()) cout << nums[i] << ",";
    // REP(i, nums.size()) if(nums[i]!=NIL)cout << nums[i] << ",";
    cout << endl;
}

bool equal(vector<int> &a, vector<int> &b) {
    int ai = 0, bi = 0;
    REP(i, a.size()) {
        while(a[ai] == NIL) ai++;
        while(b[bi] == NIL) bi++;

        // cout << "ai:" << ai << ", bi:" << bi << endl;
        if(a[ai] != b[bi]) return 0;

        i = ai + 1;
        ai++; bi++;
    }
    return 1;
}

void solve(vector<int> &nums, int d = 0) {
    // cout << "depth:" << d << endl;
    // dump(nums);

    vector<int> base = nums;
    int s = sum(nums);

    if(d > nums.size()) return;
    if(s > N) return;

    if(s == maxVal && !equal(ans[0], nums)) {
        ans.push_back(nums);
    } else if (s > maxVal) {
        ans.clear();
        ans.push_back(nums);
        maxVal = s;
    }

    REP(i, nums.size() - (d+1)) {
        merge(nums, i);
        s = sum(nums);

        if(s > N) {
            nums = base;
            continue;      // TODO: 以上？未満？
        }

        solve(nums, d+1);
        nums = base;
    }
}

int main() {
    int num;

    while(cin >> N >> num, N||num) {
        vector<int> nums;

        maxVal = -1;
        ans.clear();

        while(num > 0) {
            nums.push_back(num % 10);
            num /= 10;
        }
        reverse(nums.begin(), nums.end());

        // cout << N << "," << num << "," << ans.size() << "," << maxVal << endl;

        solve(nums);

        if(ans.size() == 0) {
            cout << "error" << endl;
        } else if (ans.size() >= 2) {
            cout << "rejected" << endl;
        } else {
            cout << sum(ans[0]);
            REP(i, ans[0].size()) if(ans[0][i] != NIL) cout << " " << ans[0][i];
            cout << endl;
        }
    }

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define INF 1e9

int t,ans,size,diff;
vector<int> a,path;
map<int,int> mp;

int calc(int idx,int N){
  int res = 0, p = 1;
  for(int i = N-1 ; i >= idx ; i--){
    res += a[i]*p;
    p *= 10;
  }
  return res;
}

void solve(int idx,int val,vector<int> &v){
  if(idx == size){
    if(val > t) return;
    if(t-val <= diff){
      diff = t-val;
      ans = val; mp[ans]++;
      path = v;
    }
    return;
  }

  for(int i = 1 ; i <= size ; i++){
    if(idx + i > size) continue;
    int c = calc(idx,idx+i);
    v.push_back(c);
    solve(idx+i,val+c,v);
    v.pop_back();
  }
}

int main(){
  int num;
  while(cin >> t >> num, (t | num)){
    mp.clear(); a.clear(); 
    diff = ans = INF;
    for(int i = 0 ; ; i++){
      if(num == 0) break;
      a.push_back(num%10);
      num /= 10;
    }
    reverse(a.begin(),a.end());
    size = a.size();
    vector<int> v;
    solve(0,0,v);
    if(diff == INF){
      cout << "error" << endl;
    }else{
      if(mp[ans] != 1){
        cout << "rejected" << endl;
      }else{
        cout << ans;
        for(int i = 0 ; i < (int)path.size() ; i++){
          cout << " " << path[i];
        }
        cout << endl;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int t, n, sum;
vector<int> ans;
bool r = false;

void dfs(vector<int> v,int n) {
    if (n < 10) {
        if (n) v.push_back(n);
        int s = 0;
        for (int i = 0; i < v.size(); i++) s += v[i];
        //for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << sum << " " << s << endl;
        if (s <= t && s > sum) {
            sum = s;
            ans = v;
            r = false;
        }
        else if (s <= t && s == sum) {
            r = true;
        }
        return;
    }
    
    int d = 1;
    while (n >= d*10) d *= 10; 
    while (1) {
        int a = n / d;
        int b = n % d;
        if (d == 1 || b*10 >= d) {
            vector<int> _v = v;
            _v.push_back(n / d);
            dfs(_v, b);
        }
        d /= 10;
        if (d == 0) break;
    }
}

int main(void){
    while (cin>>t>>n && t && n) {
        sum = -1;;
        vector<int> a;
        dfs(a, n);
        
        if (r) cout << "rejected" << endl;
        else if (sum == -1) cout << "error" << endl;
        else {
            cout << sum;
            for (int i = 0; i < ans.size(); i++) cout << " " << ans[i];
            cout << endl;
        }
    }
}

C++: #include<iostream>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;

int n,maxn;
vector<int> V,maxV;

void Shred(int sum,int m,string s)
{
  if(sum>n){
    return;
  }
  if(s.empty()){
    if(maxn==sum){
      maxV[0]=-1;
    }
    else if(maxn<sum){
      maxn=sum;
      maxV.clear();
      for(int i=0;i<V.size();i++){
	maxV.push_back(V[i]);
      }
    }
    return;
  }
  for(int i=1;i<s.length()+1;i++){
    m=atoi(s.substr(0,i).c_str());
    V.push_back(m); 
    Shred(sum+m,m,s.substr(i,s.length()));
    V.pop_back();
  }
}

int main()
{
  string s;
  int m,flag;
  while(1){
    cin>>n>>s;
    if(n==0 && s[0]=='0')break;
    maxn=0;flag=0;
    for(int i=1;i<s.length()+1;i++){
      V.clear();
      m=atoi(s.substr(0,i).c_str());
      V.push_back(m); 
      Shred(m,m,s.substr(i,s.length()));
      V.clear();
    }
    if(maxV[0]==-1)cout<<"rejected"<<endl;
    else if(maxn==0)cout<<"error"<<endl;
    else{
      cout<<maxn;
      for(int i=0;i<maxV.size();i++)cout<<" "<<maxV[i];
      cout<<endl;
    }
  }
  return 0;
}