from collections import deque from sys import stdout queue = deque([]) target, paper = raw_input().split() target = int(target) length = len(paper) best_total = 0 best_cut = [] rejected = False queue.append([[False] * length, 0, int(paper[0]), 0]) while len(queue) != 0: next = queue.popleft() cut, left, right, index = next if index == length - 1: total = left + right if total <= target: if total > best_total: best_total = total best_cut = cut[0:] rejected = False elif total == best_total: rejected = True continue queue.append([cut[0:], left, right * 10 + int(paper[index + 1]), index + 1]) cut[index] = True queue.append([cut[0:], left + right, int(paper[index + 1]), index + 1]) if best_total == 0: print 'error' elif rejected: print 'rejected' else: print best_total, stdout.write(' ') for i in range(length): stdout.write(paper[i]) if best_cut[i]: stdout.write(' ')
#include <stdio.h> #include <stdlib.h> #include <string.h> int atoi2(char* str, size_t n) { int result; char tmp = str[n]; str[n] = '\0'; result = atoi(str); str[n] = tmp; return result; } int main(void) { int t, nt, tmp, nnum; int max=0, maxi, rejectmax; char num[7]; scanf("%d %s", &t, num); for (nt=0,tmp=t;tmp>0;nt++,tmp/=10); nnum = strlen(num); for (int i=(1<<nnum-1)-1; i>=0; i--) { int sum = 0; int acc = 1; tmp = i; for (int j=nnum-1; j>0; j--) { if (tmp%2) { sum += atoi2(num+j,acc); acc = 1; } else acc++; tmp >>= 1; } sum += atoi2(num, acc); if (sum == max) { rejectmax = sum; } else if (sum > max && sum <= t) { max = sum; maxi = i; } } if (max == 0) printf("error"); else if (rejectmax == max) printf("rejected"); else { char* pnum = num; printf("%d ", max); for (int i=1<<nnum-2; i>0; i>>=1) { putchar(*pnum++); if (maxi & i) putchar(' '); } putchar(*pnum); } return 0; }
#Trailblaze import copy n, paper = raw_input().split() n = int(n) answer = 0 ans_count = 0 ans_list = [] temp_list = [] def back(currentp, sum): global n, paper, answer, ans_count global ans_list, temp_list if currentp == len(paper) and n >= sum: if answer < sum: answer = sum ans_list = copy.deepcopy(temp_list) ans_count = 1 elif answer == sum: ans_count += 1 else: return for i in range(1, len(paper) - currentp + 1): nextp = currentp + i temp_list.append(paper[currentp:nextp]) back(nextp, sum + int(paper[currentp:nextp])) temp_list.pop() return back(0, 0) if ans_count == 0: print 'error' elif ans_count == 1: print answer, for i in ans_list: print i, else: print 'rejected'