Problem:
Problem F:
Shredding Company
You have just been put in charge of developing a new shredder for the Shredding Company. Although a ``normal'' shredder would just shred sheets of paper into little pieces so that the contents would become unreadable, this new shredder needs to have the following unusual basic characteristics.
The shredder takes as input a
target number
and a sheet of paper with a number written on it.
It shreds (or cuts) the sheet into pieces each of which has one or more digits on it.
The sum of the numbers written on each piece is the closest possible number to the target number, without going over it.
For example, suppose that the target number is
50
, and the sheet of paper has the number
12346
. The shredder would cut the sheet into four pieces, where one piece has
1
, another has
2
, the third has
34
, and the fourth has
6
. This is because their sum 43 (= 1 + 2 + 34 + 6) is closest to the target number
50
of all possible combinations without going over 50. For example, a combination where the pieces are
1
,
23
,
4
, and
6
is not valid, because the sum of this combination 34 (= 1 + 23 + 4 + 6) is less than the above combination's 43. The combination of
12
,
34
, and
6
is not valid either, because the sum 52 (= 12+34+6) is greater than the target number of 50.
Figure 1. Shredding a sheet of paper having the number
12346
when the target number is
50
There are also three special rules:
If the target number is the same as the number on the sheet of paper, then the paper is not cut. For example, if the target number is
100
and the number on the sheet of paper is also
100
, then the paper is not cut.
If it is not possible to make any combination whose sum is less than or equal to the target number, then error is printed on a display. For example, if the target number is
1
and the number on the sheet of paper is
123
, it is not possible to make any valid combination, as the combination with the smallest possible sum is
1
,
2
,
3
. The sum for this combination is 6, which is greater than the target number, and thus error is printed.
If there is more than one possible combination where the sum is closest to the target number without going over it, then
rejected
is printed on a display. For example, if the target number is
15
, and the number on the sheet of paper is
111
, then there are two possible combinations with the highest possible sum of 12: (a)
1
and
11
and (b)
11
and
1
; thus rejected is printed.
In order to develop such a shredder, you have decided to first make a simple program that would simulate the above characteristics and rules. Given two numbers, where the first is the target number and the second is the number on the sheet of paper to be shredded, you need to figure out how the shredder should ``cut up'' the second number.
Input
The input consists of several test cases, each on one line, as follows:
t
1
num
1
t
2
num
2
...
t
n
num
n
0 0
Each test case consists of the following two positive integers, which are separated
...(truncated)

Submitted solutions:

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

//repetition
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)

//container util
#define all(x) (x).begin(),(x).end()

//typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

//const value
//const ll MOD = 1e9 + 7;
//const int dx[] = {0,1,0,-1};//{0,0,1,1,1,-1,-1,-1};
//const int dy[] = {1,0,-1,0};//{1,-1,0,1,-1,0,1,-1};

//conversion
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline ll toLL(string s) {ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
ll t;
string num;
ll  ans;
int dp[1010101];
void dfs(int current,int amari, int cnt){
  if(current == num.size() && cnt == 0){
    dp[amari]++;
    return;
  }

  // 区切る場合
  int mm = cnt*10 + (num[current]-'0');
  if(amari - mm >= 0) dfs(current+1, amari-mm, 0);
  else return;

  // 区切らない場合
  if(current+1 < num.size()) dfs(current+1, amari, mm);

}

VI ansDFS(int current, int amari, int cnt, VI v){
  VI notAns;
  if(current == num.size()){
    if(ans == amari && cnt == -1) {
      return v;
    }
    else return notAns;
  }
  cnt = max(0,cnt);
  VI ansV0;
  VI ansV1;

  int mm = cnt*10 + (num[current]-'0');
  if(amari - mm < 0) return notAns;

  // 区切らない場合
  if(not(current > 0 && num[current] == '0' && num[current-1] == '0')) ansV0 = ansDFS(current+1, amari, mm, v);

  // 区切る場合
  v.push_back(mm);
  // cout << mm << endl;
  ansV1 = ansDFS(current+1, amari-mm, -1,v);

  if(ansV0.size() > 0) return ansV0;
  else return ansV1;
}


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  while(true){
    cin >> t >> num;
    if(t == 0 && num == "0") break;


    rep(j,1010101) dp[j] = 0;
    dfs(0,t,0);
    bool flag = false;

    rep(i,t+1){
      if(dp[i] == 1){
        ans = i;

        VI v;
        VI ansV = ansDFS(0, t ,0, v);
        ans = t - ans;
        cout << ans << " ";
        // cout << "Vsize is " << ansV.size() << endl;
        rep(j,ansV.size()){
          if(ansV.size() == j+1) cout << ansV[j] << endl;
          else cout << ansV[j] << " ";
        }

        flag = true;
        break;
      }else if(dp[i] > 1){
        cout << "rejected" << endl;
        flag = true;
        break;
      }
    }
    if(flag == false){
      cout << "error" << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 7072
final_rank: 4

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


int main(){
    int t;
    string num;
    while(cin>>t>>num,t!=0 || num!="0"){
        vector<vector<int>> dp(num.size()+1,vector<int>(t+1,0));
        dp[0][0]=1;
        for(int i=0;i<num.size();i++){
            for(int j=0;j<=t;j++){
                for(int k=i+1;k<=num.size();k++){
                    //cut num[i,k)
                    int cutnum=stoi(num.substr(i,k-i));
                    if(cutnum+j<=t) dp[k][j+cutnum]+=dp[i][j];
                }
            }
        }
        for(int i=t;i>=-1;i--){
            if(i==-1){
                cout<<"error"<<endl;
                break;
            }
            if(dp[num.size()][i]){
                if(dp[num.size()][i]>=2){
                    cout<<"rejected"<<endl;
                }
                else{
                    vector<string> res;
                    int pos=num.size();
                    int val=i;
                    while(pos>0){
                        for(int j=0;j<pos;j++){
                            //cut num[j,pos)
                            int v=stoi(num.substr(j,pos-j));
                            if(val-v>=0 && dp[j][val-v]==1){
                                res.push_back(num.substr(j,pos-j));
                                pos=j;
                                val-=v;
                                break;
                            }
                        }
                    }
                    cout<<i<<" ";
                    reverse(res.begin(),res.end());
                    for(int j=0;j<res.size();j++){
                        cout<<res[j] <<(j+1==res.size() ? "\n" : " ");
                    }
                }
                break;    
            }
        }
        
    }

    return 0;
}
accuracy: 1
time_ms: 1490
memory_kb: 29188
final_rank: 10

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n; string s;
    while (cin >> n >> s, n || s != "0") {
        ll m = linf;
        vector<ll> ans;
        bool rejected = false;
        try {
            set<vector<ll>> used;
            rep(a, 7) rep(b, 7) rep(c, 7) rep(d, 7) rep(e, 7) rep(f, 7) {
                if (a + b + c + d + e + f != s.size()) continue;
                vector<ll> v;
                if (a) v.pb(a);
                if (b) v.pb(b);
                if (c) v.pb(c);
                if (d) v.pb(d);
                if (e) v.pb(e);
                if (f) v.pb(f);
                if (used.count(v) > 0) continue;
                used.insert(v);
                ll cnt = 0;
                vector<ll> vals;
                bool flag = true;
                rep(i, v.size()) {
                    string str = s.substr(cnt, v[i]);
                    if (str.size() > 1 && str[0] == '0') {
                        // flag = false;
                        // break;
                    }
                    vals.pb(stoll(str));
                    cnt += v[i];
                }
                if (!flag) continue;
                ll sum = 0;
                rep(i, vals.size()) sum += vals[i];
                if (sum > n) continue;
                if (n - sum == m) {
                    rejected = true;
                }
                else if (n - sum < m) {
                    rejected = false;
                    m = n - sum;
                    ans = vals;
                }
            }
            if (rejected) throw string("rejected");
            if (m == linf) throw string("error");
            cout << n - m << " " << ans << endl;
        }
        catch (string err) {
            cout << err << endl;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3308
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int mypow(int x, int y) {
    int ret = 1;
    while(y > 0) {
        ret *= x;
        y--;
    }
    return ret;
}
int main() {
    int t, num;
    while(cin >> t >> num, t) {
        vector<vector<vector<int>>> ret(1000000);
        ret[num].push_back({num});

        for(int i=1; i<(1<<5); ++i) {
            int top = 0;
            for(int j=4; j>=0; --j) {
                if((i >> j) % 2 == 1) {
                    top = j;
                    break;
                }
            }
            if(mypow(10, top+1) > num) {
                break;
            }
            int rest = num;
            vector<int> v2;
            int sum=0;
            int prev_b = -1;
            for(int b=0; b<5; ++b) {
                if((i >> b) % 2 == 1) {
                    int tmp = 0;
                    if(prev_b != -1) {
                        tmp = mypow(10, b - prev_b);
                    } else {
                        tmp = mypow(10, b+1);
                    }
                    v2.push_back(rest % tmp); // todo
                    sum += rest % tmp;
                    rest /= tmp;
                    prev_b = b;
                    if(rest == 0) {
                        break;
                    }
                }
            }
            if(rest != 0) {
                v2.push_back(rest);
            }
            sum += rest;
            //cout << "rest = " << rest << "   sum=" << sum << endl;
            //cout << endl;
            //for(auto x : v2) {
            //    cout << x << endl;
            //}
            //cout << endl;
            if(sum == 0) {
                continue;
            }
            ret[sum].push_back(v2);
        }

        bool finded = false;
        for(int i=t; i>=0; --i) {
            if(ret[i].size() >= 2) {
                cout << "rejected" << endl;
                finded = true;
                break;
            } else if(ret[i].size() == 1) {
                finded = true;
                cout << i << " ";
                reverse(ret[i][0].begin(), ret[i][0].end());
                for(int j=0; j<ret[i][0].size()-1; ++j) {
                    cout << ret[i][0][j] << " ";
                }
                cout << ret[i][0][ret[i][0].size()-1] << endl;
                break;
            }
        }
        if(!finded) {
            cout << "error" << endl;
        }
    }
}
accuracy: 1
time_ms: 470
memory_kb: 26564
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
using namespace std;
typedef pair<int, vector<string> > P;

int t;
string s;

P f(string s, vector<bool> cut) {
	P ret;
	string a;
	a += s[0];
	for(int i = 0; i < cut.size(); i++) {
		if(cut[i]) {
			ret.second.push_back(a);
			a = s[i + 1];
		}
		else {
			a += s[i + 1];
		}
	}
	ret.second.push_back(a);
	for(int i = 0; i < ret.second.size(); i++) {
		a = ret.second[i];
		while(a.size() > 1 && a.front() == '0') a = a.substr(1, a.size() - 1);
		ret.first += atoi(a.c_str());
	}
	return ret;
}

vector<vector<string> > ans[1000000];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	while(cin >> t >> s, t && s != "0") {
		for(int i = 0; i < 1000000; i++) {
			ans[i].clear();
		}
		int n = s.size();
		int m = -1;
		for(int bit = 0; bit < 1 << (n - 1); bit++) {
			vector<bool> cut(n - 1);
			for(int i = 0; i < n - 1; i++) {
				if(bit >> i & 1) cut[i] = true;
			}
			P p = f(s, cut);
			if(p.first > t) continue;
			m = max(m, p.first);
			ans[p.first].push_back(p.second);
		}
		if(m == -1) {
			cout << "error" << endl;
		}
		else if(ans[m].size() >= 2) {
			cout << "rejected" << endl;
		}
		else {
			cout << m;
			for(string v : ans[m][0]) {
				cout << " " << v;
			}
			cout << endl;
		}
	}
}
accuracy: 1
time_ms: 310
memory_kb: 26644
final_rank: 6

code_snippet: #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;
    t=min(t,n);
    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;
}
accuracy: 1
time_ms: 760
memory_kb: 35300
final_rank: 9

code_snippet: #include <iostream>
#include <string>
#include <vector>

using namespace std;

unsigned short dp[7][1000000];
unsigned char pr[7][1000000];
int df[7][1000000];

int main(){
	while(true){
		int n;
		string s;
		cin >> n >> s;
		if(n == 0 && s == "0"){ break; }
		const int m = s.size();
		for(int i = 0; i <= m; ++i){
			for(int j = 0; j <= n; ++j){
				dp[i][j] = pr[i][j] = df[i][j] = 0;
			}
		}
		dp[0][0] = 1;
		for(int i = 0; i < m; ++i){
			int x = 0;
			for(int j = i; j < m; ++j){
				x = (x * 10) + s[j] - '0';
				if(x > n){ break; }
				for(int k = 0; k + x <= n; ++k){
					if(dp[i][k] == 0){ continue; }
					dp[j + 1][k + x] += dp[i][k];
					pr[j + 1][k + x] = i;
					df[j + 1][k + x] = x;
				}
			}
		}
		int answer = -1;
		for(int i = n; answer < 0 && i >= 0; --i){
			if(dp[m][i] != 0){ answer = i; }
		}
		if(answer < 0){
			cout << "error" << endl;
		}else if(dp[m][answer] >= 2){
			cout << "rejected" << endl;
		}else{
			cout << answer;
			vector<int> cut;
			int r = m, c = answer;
			do {
				const int d = df[r][c];
				cut.push_back(d);
				r = pr[r][c];
				c -= d;
			} while(r > 0);
			for(int i = cut.size() - 1; i >= 0; --i){
				cout << " " << cut[i];
			}
			cout << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 48764
final_rank: 8

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <ctime>
#include <list>
#include <numeric>
#include <fstream>

using namespace std;
static const double EPS = 1e-8;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> PI;
#ifndef M_PI
const double M_PI=acos(-1.0);
#endif
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FORR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int((a).size()))
#define F first
#define S second
void pkuassert(bool b){if(!b){int a=0;cout << 1/a << endl;}};
int dx[]={0,-1,0,1,1,1,-1,-1},dy[]={1,0,-1,0,1,-1,1,-1};

string in;

int num(int s,int t){
  int ret=0;
  rep(i,t-s) ret=ret*10+in[s+i]-'0';
  return ret;
}
int memo[6][1000000];

int rec(int ne,int les,bool pr=false){
  if(les<0) return 0;
  if(ne==SZ(in)) return les==0;
  if(!pr && memo[ne][les]!=-1) return memo[ne][les];
  if(num(ne,SZ(in))<les) return 0;
  int& ret=memo[ne][les]=0;
  
  for(int i=ne+1;i<=SZ(in);++i){
    ret += rec(i,les-num(ne,i));
    if(pr && ret){
      cout << ' ' << in.substr(ne,i-ne);
      rec(i,les-num(ne,i),true);
      return 0;
    }
    if(ret>1) return ret=2;
  }
  return ret;
}

int t;
void solve(){
  memset(memo,-1,sizeof(memo));
  while(t){
    if(rec(0,t)==1){
      cout << t;
      rec(0,t,true);
      cout << endl;
      return;
    }
    if(rec(0,t)>1){
      cout << "rejected" << endl;
      return;
    }
    --t;
  }
  cout << "error" << endl;
}

int main(){
  while(cin >> t >> in && t && in!="0") solve();
}
accuracy: 1
time_ms: 100
memory_kb: 24660
final_rank: 5

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
using namespace std;

#define INF (1<<29)

int toInt(string s){
  istringstream iss(s);
  int a;
  iss >> a;
  return a;
}

int main(){
  int n;
  string s;
  while(cin >> n >> s){
    if(n == 0 && s == "0") break;
    int ans = -1;
    vector<string> ans2;
    map<int, int> is_found;
    
    for(int i = 0 ; i < (1<<s.size()) ; i++){
      vector<string> tmp;
      int pos = 0;
      int point = 0;
      for(int j = 0 ; j < s.size() ; j++){
	if(j == s.size()-1){
	  string t = s.substr(pos, j-pos+1);
	  tmp.push_back(t);
	  pos = j+1;
	  point += toInt(t);
	}
	
	else if((1<<j) & i){
	  string t = s.substr(pos, j-pos+1);
	  tmp.push_back(t);
	  pos = j+1;
	  point += toInt(t);
	}
      }
      
      is_found[point]++;
      if(point <= n && n-point < n-ans){
	ans = point;
	ans2 = tmp;
      }
      /*
      cout << point << ' ';
      for(int b = 0 ; b < tmp.size() ; b++){
	cout << tmp[b] << ' ';
      }
      cout << endl;
      */
    }
    
    //cout << is_found[ans] << endl;
    bool f = true;
    if(is_found[ans] > 2) f = false;
    
    if(ans == -1) cout << "error" << endl;
    else if(!f){
      cout << "rejected" << endl;
    }
    else{
      cout << ans << ' ';
      for(int i = 0 ; i < ans2.size() ; i++){
	cout << ans2[i];
	if(i != ans2.size()-1) cout << ' ';
	else cout << endl;
      }
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1228
final_rank: 1

code_snippet: #include<iostream>
#include<sstream>

using namespace std;

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

int main() {
  while (true) {
    int t;
    string n;
    cin >> t >> n;
    if (t == 0 && n == "0") break;
    int res = 0, b;
    rep (i, 1 << (n.size() - 1)) {
      int p = 0, r = 0;
      rep (j, n.size() - 1) {
	if (i & 1 << j) {
	  stringstream ss;
	  ss << n.substr(p, j - p + 1);
	  int a;
	  ss >> a;
	  r += a;
	  p = j + 1;
	}
      }
      stringstream ss;
      ss << n.substr(p);
      int a;
      ss >> a;
      r += a;
      if (r <= t && res < r) {
	res = r;
	b = i;
      } else if (res == r) {
	b = -1;
      }
    }
    if (res == 0) cout << "error" << endl;
    else if (b == -1) cout << "rejected" << endl;
    else {
      cout << res << " " << n[0];
      rep (i, n.size() - 1) {
	if (b & 1 << i) cout << " ";
	cout << n[i + 1];
      }
      cout << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1236
final_rank: 2