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 <vector>
#include <string>
#include <map>
#include <algorithm>

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

using namespace std;

int dig(int n) {
  if(n<10)return 1;
  else return dig(n/10)+1;
}

int main() {
  while(1){
    int t,n;
    cin>>t>>n;
    if(!n)break;
    int s=dig(n);
    string ns=to_string(n);
    vector<vector<int>> dp(s+1,vector<int>(n+1));
    dp[0][0] = 1;
    REP(i,s+1){
      REP(j,i){
        auto sub=ns.substr(j,i-j);
        int m=stoi(sub);
        REP(k,n-m+1){
          if (dp[i][k+m] == 0 && dp[j][k] > 0)
            dp[i][k+m] = j+1;
          else if ((dp[i][k+m] != 0 && dp[j][k] != 0) || dp[j][k] < 0)
            dp[i][k+m] = -1;
        }
      }
    }
    bool ok=false;
    for (int u=t;u>0;--u) {
      if (dp[s][u] > 0) {
        vector<int> res;
        int i=s;
        int j=u;
        do {
          int oldi=i;
          i=dp[i][j]-1;
          auto sub=ns.substr(i,oldi-i);
          res.push_back(stoi(sub));
          j-=stoi(sub);
        } while(i>0);
        reverse(begin(res),end(res));
        cout<<u;
        for(int v:res) cout<<' '<<v;
        cout<<endl;
        ok = true;
        break;
      } else if (dp[s][u] < 0) {
        cout<<"rejected"<<endl;
        ok = true;
        break;
      }
    }
    if (!ok)
      cout<<"error"<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<set>

using namespace std;

int comb(int n, int k, int target, string num, set< vector<string> > &parts){
  int *p;
  bool loop = false;
  int min_diff = target + 1;
  
  p = new int[k];
  for(int i = 0; i < k; i++) p[i] = i;
  
  do{ loop = false;
    
    // TODO
    int sum = 0;
    int st = 0;
    string s;
    vector<string> vp;
    for(int i = 0; i < k; ++i){
      //cout << num.substr(st,p[i]-st+1) << '@';
      s = num.substr(st,p[i]-st+1);
      if(s.length()>0)
	vp.push_back(s);
      st = p[i]+1;
    }
    if(st<n){
      s = num.substr(st,n-st+1);
      if(s.length()>0)vp.push_back(s);
    }

    for(int i = 0; i < (int)vp.size(); ++i){
      sum += atoi(vp[i].c_str());
      //cout << vp[i] << '@';
    }
    //cout << endl;
    //cout << sum << endl;
    if( target - sum >= 0 && min_diff > target - sum ){
      min_diff = target - sum;
      parts.clear();
      parts.insert(vp);
    }else if( target - sum >= 0 && min_diff == target - sum ){
      parts.insert(vp);
    }
    
    for(int pk=k-1;pk>=0;pk--){p[pk]++;if(p[pk]<=n-(k-pk)){for(int j=pk+1;j<k;j++)p[j]=p[j-1]+1;pk++;loop=true;break;}}
  }while(loop);
  delete [] p;
  return min_diff;
}

int solve(int target, string num, set< vector<string> > &parts){
  int len = num.length();
  int min = target + 1;
  for(int part = 1; part <= len; ++part){
    set< vector<string> > tmp_parts;
    int t = comb(len,part,target,num,tmp_parts);
    if( min > t ){
      min = t;
      parts = tmp_parts;
    }else if( min == t ){
      for(set< vector<string> >::iterator it = tmp_parts.begin(); it != tmp_parts.end(); ++it){
	parts.insert( *it );
      }
    }
  }
  return min;
}

int main()
{
  while(true){
    int target;
    string num;
    set< vector<string> > parts;

    cin >> target >> num;
    if( target == 0 && num == "0" ) break;
    int res = solve( target, num, parts );
    if( res == target + 1 ){
      cout << "error" << endl;
    }else if( parts.size() > 1 ){
      cout << "rejected" << endl;
    }else{
      cout << target - res;
      vector<string> p = *(parts.begin());
      for(int i = 0; i < (int)p.size(); ++i){
	cout << ' ' << p[i];
      }
      cout << endl;
    }
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

int main() {
    while (1) {
        int t;
        string s;
        cin >> t;
        cin >> s;
        if (t == 0) {
            break;
        }
        int len = s.size();
        int ansnum = -1;
        int anscut = -1;
        bool ok = true;
        for (int i = 0; i < (1 << (len - 1)); i++) {
            int sum = 0;
            string cur = "";
            cur += s[0];
            for (int j = 1; j < len; j++) {
                if (i & (1 << (j - 1))) {
                    sum += stoi(cur);
                    cur = s[j];
                } else {
                    cur += s[j];
                }
            }
            sum += stoi(cur);
            if (sum <= t && sum > ansnum) {
                ansnum = sum;
                anscut = i;
                ok = true;
            } else if (sum <= t && sum == ansnum) {
                ok = false;
            }
        }
        if (ansnum == -1) {
            cout << "error" << endl;
        } else {
            if (!ok) {
                cout << "rejected" << endl;
            } else {
                cout << ansnum << " ";
                string cur = "";
                cur += s[0];
                for (int j = 1; j < len; j++) {
                    if (anscut & (1 << (j - 1))) {
                        cout << cur << " ";
                        cur = s[j];
                    } else {
                        cur += s[j];
                    }
                }
                cout << cur << endl;
            }
        }
    }
}