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 <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <queue>
#include <set>
#include <cstdio>
#include <climits>
#include <cmath>
#include <array>
#include <functional>
#include <sstream>
#include <list>
#include <iomanip>
#include <set>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;
struct Point
{
  double x,y;
  Point(){x=0;y=0;}
  Point(double d_x,double d_y){x=d_x,y=d_y;}
  double operator*(Point obj){return obj.x*x+obj.y*y;}
  double operator%(Point obj){return obj.y*x-obj.x*y;}
  Point operator*(double b){Point tmp;tmp.x=x*b;tmp.y=y*b;return tmp;}
  Point operator/(double b){Point tmp;tmp.x=x/b;tmp.y=y/b;return tmp;}
  Point operator+(Point obj){Point tmp;tmp.x=x+obj.x;tmp.y=y+obj.y;return tmp;}
  Point operator-(){Point tmp;tmp.x=-x;tmp.y=-y;return tmp;}
  Point operator-(Point obj){Point tmp;tmp.x=x-obj.x;tmp.y=y-obj.y;return tmp;}
  Point operator-=(Point obj){x-=obj.x;y-=obj.y;return *this;}
  Point operator+=(Point obj){x+=obj.x;y+=obj.y;return *this;}
  Point operator/=(double b){x=x/b;y=y/b;return *this;}
  Point operator*=(double b){x=x*b;y=y*b;return *this;}
  double size(){return hypot(x,y);}
  Point unit(){return Point(x/size(),y/size());}
  Point normal(){return Point(y,-x);}
  double atan(){return atan2(y,x);}
};

bool operator<(Point a,Point b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool operator>(Point a,Point b){return b<a;}
bool operator<=(Point a,Point b){return !(b<a);}
bool operator>=(Point a,Point b){return !(a<b);}
bool operator==(Point a,Point b){return (a-b).size()<eps;}
bool operator!=(Point a,Point b){return !(a==b);}

bool equal(double a,double b){return abs(a-b)<eps;}
double cross(Point a,Point b){return a%b;}
double dot(Point a,Point b){return a*b;}

int ccw(Point a,Point b,Point c)
{
  b=b-a;
  c=c-a;
  if(b%c>0) return +1;
  else if(b%c<0)return -1;
  else if(b*c<0) return +2;
  else if(b.size()<c.size()) return -2;
  else return 0;
}

int target,digit,ans;
vi comb;
vi num[6];
bool reject,error;

int convert(string s)
{
  int val=0;
  for(int i=0;i<s.size();i++)
    {
      val=10*val+(int)(s[i]-'0');
    }
  return val;
}

void dfs(int sum,int idx,vector<int> select)
{
  if(target<sum) return;
  else if(idx==digit)
    {
      error=false;
      if(ans<sum)
	{
	  reject=false;
	  ans=sum;
	  comb=select;
	}
      else if(ans==sum) reject=true;
    }
  else
    {
      for(int j=0;j<num[idx].size();j++)
	{
	  vi tmp=select;
	  tmp.push_back(num[idx][j]);
	  dfs(sum+num[idx][j],idx+j+1,tmp);
	}
    }
}

int main(int argc,char const* argv[])
{
  string tmp;
  while(1)
    {
      cin >> target >> tmp;
      if(!target && tmp=="0") break;
      ans=-1;
      error=true;
      reject=false;
      comb.clear();
      for(int i=0;i<6;i++)
	{
	  num[i].clear();
	}
      digit=tmp.size();
      if(target==convert(tmp))
	{
	  cout << target << ' ' << target << endl;
	  continue;
	}
      for(int i=0;i<digit;i++)
	{
	  for(int j=0;i+j<digit;j++)
	    {
	      num[i].push_back(convert(tmp.substr(i,j+1)));
	      //cout << num[i][j] << ' ';
	    }
	}
      dfs(0,0,vector<int>());
      if(reject) cout << "rejected" << endl;
      else if(error) cout << "error" << endl;
      else
	{
	  cout << ans;
	  for(int i=0;i<comb.size();i++)
	    {
	      cout << ' ' << comb[i];
	    }
	  cout << endl;
	}
    }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <bitset>
#include <vector>

using namespace std;

#define max 6

int main(void){
	int div[] = {1, 10, 100, 1000, 10000, 100000, 1000000 };
	//FILE *fp_in = freopen("data.txt", "r", stdin);

	while(1) {
		int t, num, lim, indx = -1, summax = 0;
		bool error = true, rejected = false;
		stringstream ss;
		vector<int> bits;	//ビット列を記憶させる
		vector< vector< int > > vs;

		cin >> t >> num;
		ss << num;
		if( !( t | num ) ) break;

		lim = ( 1 << ( ss.str().length() - 1 ) ) - 1;

		for(int i = 0; i <= lim; i++) {
//			cout << i << ": ";
			vector< int > v;
			int temp = 0;
			for(int j = 0; j < max; j++) {
				if( i & ( 1 << j ) ) {
//					cout << ( num % div[j + 1] ) / div[temp] << " ";
					v.push_back( ( num % div[j + 1] ) / div[temp] );
					temp = j + 1;
				}
			}
			v.push_back( num / div[temp] );
			vs.push_back( v );
		}

		for(int i = 0; i < vs.size(); i++) {
			int sum = 0;
			for(int j = 0; j < vs[i].size(); j++) {
				sum += vs[i][j];
			}
			if( sum <= t ) {
				error = false;
				if( sum == summax ) {
					rejected = true;
				} else if( sum > summax ) {
					indx = i;
					summax = sum;
					rejected = false;
				}
			}
		}

		if( error == true ) {
			cout << "error" << endl;
		} else if( rejected == true ) {
			cout << "rejected" << endl;
		} else {
			cout << summax;
			for(int i = vs[indx].size() - 1; i >= 0; i--)
				cout << " " << vs[indx][i];
			cout << endl;
		}


	}
	//while(1){}

	return 0;
}