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

using namespace std;

int size, valc, n;
string str;
vector<int> ans, path;
bool reject;

void init(){
  reject = false;
  ans.clear();
  path.clear();
  valc = -1;
  size = str.size();
}

void solve(int p){
  if(p == size){
    int sum = 0;
    for(int i=0;i<path.size();i++) sum += path[i];
    if(sum == valc){
      reject = true;
    }else if(valc < sum && sum <= n){
      valc = sum;
      ans = path;
      reject = false;
    }
  }else{
    for(int i=1;i+p<=size;i++){
      int tmp = atoi((str.substr(p, i)).c_str());
      path.push_back(tmp);
      solve(p+i);
      vector<int>::iterator ite = path.end();
      path.erase(--ite);
    }
  }
}

main(){
  while(cin >> n >> str){
    if(n == 0 && str == "0") break;
    init();
    solve(0);
    if(valc == -1) cout << "error" << endl;
    else if(reject) cout << "rejected" << endl;
    else{
      cout << valc;
      for(int i=0;i<ans.size();i++){
        cout << ' ' << ans[i];
      }
      cout << endl;
    }
  }
}

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

string solve(int t, string& num) {
    int n = num.size();

    string ans = "error";
    int max_v = 0;
    for (int j = 0; j < (1 << n); j += 2) {
        stringstream ss, tt;
        int sum = 0, a = num[0] - '0';
        for (int k = 1; k < n; ++k) {
            if ((j >> k) & 1 != 0) {
                a = 10 * a + (num[k] - '0');
            } else {
                sum += a;
                ss << a << ' ';
                a = num[k] - '0';
            }
        }
        ss << a;
        sum += a;
        if (max_v <= sum && sum <= t) {
            if (sum == max_v) {
                ans = "rejected"; 
            } else {
                max_v = sum;
                tt << sum << " " << ss.str();
                ans = tt.str();
            }
        }
    }
    return ans;
}

int main() {
    int t, n;
    string num;
    while (true) {
        cin >> t >> num;
        if (t == 0 && num == "0") {
            break;
        }
        cout << solve(t, num) << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;

const int INF = 1 << 28;
int sum;
string num;

int main() {
    while (cin >> sum >> num, sum || num != "0") {
        map<int,vec> m;
        int target = -1;
        for (int i = 0; i < 1 << (num.size() - 1); i++) {
            int tmp = 0;
            int pre = 0;
            for (int j = 0; j < num.size() - 1; j++) {
                if (i & (1 << j)) {
                    string s = string(num.begin() + pre, num.begin() + j + 1);
                    stringstream ss; ss << s;
                    int n; ss >> n;
                    tmp += n;
                    pre = j + 1;
                }
            }

            string s = string(num.begin() + pre, num.end());
            stringstream ss; ss << s;
            int n; ss >> n;
            tmp += n;

            if (tmp <= sum) {
                m[tmp].push_back(i);
                target = max(target, tmp);
            }
        }

        if (m.size() == 0) {
            cout << "error" << endl;
        } else if (m[target].size() == 1) {
            cout << target << " ";
            int tmp = m[target][0];
            for (int i = 0; i < num.size(); i++) {
                cout << num[i];
                if (tmp & (1 << i)) cout << " ";
            }
            cout << endl;
        } else {
            cout << "reject" << endl;
        }
    }
}

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

int toInt(string s)
{
	int res = 0;
	for(int i=0; i<s.size(); i++) {
		res *= 10;

		res += s[i]-'0';
	}

	return res;
}

int res,cnt;
vector<int> pat;

void solve(int T, int n, int s, vector<int>& a, string seq)
{
	if(n == seq.size()) {
		if(s == res) cnt++;
		else if(s > res) {
			cnt = 0;
			res = s;
			pat = a;
		}
	}

	for(int i=1; i<=6; i++) {
		if(seq.size() - n < i) continue;
		int num = toInt(seq.substr(n, i));
		int sum = s + num;

		if(sum > T) continue;

		a.push_back(num);
		solve(T, n+i, sum, a, seq);
		a.pop_back();
	}
}

int main()
{
	int T;
	string N;
	while(cin >> T >> N, (T||N!="0")) {
		res = -1;
		cnt = 0;
		vector<int> a;

		solve(T,0,0,a,N);

		if(res == -1) cout << "error" << endl;
		else if(cnt >= 1) cout << "rejected" << endl;
		else { 
			cout << res;
			for(int i=0; i<pat.size(); i++)
				cout << " " << pat[i];

			cout << endl;
		}
	}

}