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>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;

string s, ans_str;
int n, ans_num, ans_num2;
bool f[8];

typedef string::const_iterator State;
int number(State&);
int factor(State&);
int term(State&);
int expression(State&);

// ??°??????????????????????????????????????°????????????
int number(State &begin) {
    int ret = 0;

    while (isdigit(*begin)) {
        ret *= 10;
        ret += *begin - '0';
        begin++;
    }

    return ret;
}

// ??¬??§?????°???????????????????????????????????????????????????
int factor(State &begin) {
    if (*begin == '(') {
        begin++; // '('????£???°??????
        int ret = expression(begin);
        begin++; // ')'????£???°??????
    } else {
        return number(begin);
    }
    return INF;
}

// ?????????????????????????????????????????????????????????????????????
int term(State &begin) {
    int ret = factor(begin);

    for (;;) {
        if (*begin == '*') {
            begin++;
            ret *= factor(begin);
        } else if (*begin == '/') {
            begin++;
            ret /= factor(begin);
        } else {
            break;
        }
    }

    return ret;
}

// ?????????????????????????????????????????????????????????????????????
int expression(State &begin) {
    int ret = term(begin);

    for (;;) {
        if (*begin == '+') {
            begin++;
            ret += term(begin);
        } else if (*begin == '-') {
            begin++;
            ret -= term(begin);
        } else {
            break;
        }
    }

    return ret;
}

void solve(string p, int i){
    if(i == s.size()){
        State it = p.begin();
        int temp = expression(it);
        if(abs(n - ans_num) > abs(n - temp) && n >= temp){
            ans_str = p;
            ans_num = temp;
        }else if(abs(n - ans_num) == abs(n - temp)){
            ans_num2 = temp;
        }
        return;
    }

    p+=s[i];
    solve(p, i + 1);
    if(p[i + 1] != '0' && i + 1 != s.size() && f[i] == 0 && f[i + 1] == 0){
        p+='+';
        solve(p, i + 1);
    }
}

void check(){
    string temp;
    int m = static_cast<int>(log10(n));

    rep(i,s.size() - m){
        string temp = s.substr(i,m);
        State it = s.begin();
        if(number(it) == n){
            range(j,i,i + m){
                f[j] = 1;
            }
        }
    }
}

int main(){
    while(cin >> n >> s, n||s[0] != '0'){
        ans_num = -1;
        rep(i,8) f[i] = 0;

        solve("", 0);

        State it = s.begin();
        if(ans_num == -1){
            cout << "error" << endl;
        }else if(ans_num2 == ans_num){
            cout << "rejected" << endl;
        }else{
            cout << ans_num << ' ';
            rep(i,ans_str.size()){
                if(ans_str[i] == '+') cout << ' ';
                else cout << ans_str[i];
            }
            cout << endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int retdigit(int s)
{
	int d = 1;

	while (s / 10 != 0) {
		s /= 10;
		d++;
	}

	return d;
}

int mypow(int x, int y)
{
	int r = 1;

	rep(i, y) {
		r *= x;
	}

	return r;
}

int vec_sum(vector<int> vec)
{
	int sum = 0;

	rep(i, vec.size()) {
		sum += vec[i];
	}

	return sum;
}

int main()
{
	while (true) {
		int t, s;

		cin >> t >> s;

		if (t == 0 && s == 0) {
			break;
		}

		int digit = retdigit(s);
		int dif = 10000000;
		int ans;
		bool errorflag = true;
		bool rejectflag = false;
		vector<int> ans_splits;

		rep(i, (1 << (digit - 1))) {
			int temp = s;
			vector<int> splits;
			int count = 0;

			rep(j, digit - 1) {
				if ((i >> j) % 2 == 1) {
					splits.push_back(temp%mypow(10, count + 1));
					temp /= mypow(10, count + 1);
					count = 0;
				}
				else {
					count++;
				}
			}

			splits.push_back(temp);

			int split_sum = vec_sum(splits);

			if (split_sum > t) {
				continue;
			}
			
			if (t - split_sum < dif) {
				dif = t - split_sum;
				ans = split_sum;
				ans_splits = splits;
				errorflag = false;
				rejectflag = false;
			}
			else if (t - split_sum == dif) {
				rejectflag = true;
			}
		}

		if (errorflag == true) {
			cout << "error" << endl;
		}
		else if (rejectflag == true) {
			cout << "rejected" << endl;
		}
		else {
			cout << ans;

			for (int i = ans_splits.size() - 1; i >= 0; i--) {
				cout << " " << ans_splits[i];
			}

			cout << endl;
		}

	}
	
	return 0;
}