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 <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

struct before_main{before_main(){cin.tie(0); ios::sync_with_stdio(false);}} before_main;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

int main() {

  int t; string num;
  for(; cin >> t >> num;) {
    if(t == 0 && num == "0") { break; }
    if(t == stoi(num)) {
      cout << t << " " << t << endl;
      continue;
    }

    int n = num.size();

    int asum = -1;
    int dcnt = 0;
    deque<int> part;
    std::function<void(int, int, deque<int>&)> divide = [&](int x, int idx, deque<int>& dl) {
      if(idx == n) {
        if(t >= x) {
          if(maximize(asum, x)) {
            dcnt = 0;
            part = dl;
          } else {
            if(asum == x) {
              dcnt ++;
            }
          }
        }
        return;
      }

      REP(i, idx, n) {
        dl.push_back(i+1);
        divide(x + stoi(string(num.begin() + idx, num.begin() + i+1)), i+1, dl);
        dl.pop_back();
      }
    };

    deque<int> dl = {0};
    divide(0, 0, dl);

    if(dcnt > 0) {
      cout << "rejected" << endl;
    }
    else if(asum == -1) {
      cout << "error" << endl;
    }
    else {
      cout << asum;
      rep(i, part.size()-1) {
        cout << " " << string(num.begin() + part[i], num.begin() + part[i+1]);
      }
      cout << endl;
    }

  }
  
  return 0;
}

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

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, SZ(s)) {
    cout << s[i];
    if(i == SZ(s)-1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

int main () {
  cin.tie(0);
  cout << fixed << setprecision(10);

  while(1) {
    int t; string s; cin >> t >> s;
    if(t == 0 && s == "0") break;
    int n = s.length();
    bool reject = false;
    int sum = -INF;
    vector<int> res;
    for(int i=0;i<(1<<(n-1));i++) {
      int stmp = 0, tmp = 0;
      bool update = false;
      vector<int> v;
      for(int j=0;j<n;j++) {
        update = true;
        tmp *= 10;
        tmp += (s[j] - '0');
        if((i>>j) & 1) {
          stmp += tmp;
          v.push_back(tmp);
          tmp = 0;
          update = false;
        }
      }
      if(update) {
        stmp += tmp;
        v.push_back(tmp);
      }
      if(stmp < sum || t < stmp) {
        continue;
      }
      if(stmp == sum) {
        reject = true;
      } else {
        reject = false;
        res = v;
        sum = stmp;
      }
    }
    if(sum == -INF) {
      cout << "error" << endl;
    } else if(reject) {
      cout << "rejected" << endl;
    } else {
      cout << sum;
      for(int i=0;i<(int)res.size();i++) {
        cout << " " << res[i];
      }
      cout << endl;
    }
  }
}