Problem:


ブラックジャック

ブラックジャックはカジノで行われるカードゲームの一種で、1 〜 13  までの数が書かれたカードを使ってゲームが行われます。各カードの点数は次のように決まっています。

1 は 1 点あるいは 11 点

2 から 9 までは、書かれている数の通りの点数

10 から 13 までは、10 点

このゲームには親を含めた何人かの参加者がおり、それぞれが何枚かのカードの組を持っています。このカードの組のことを手と呼びます。手の点数はカードの点数の合計です。その計算は次のように行うものとします。

カードの点数の合計が 21 より大きくなるときは、手の点数を 0 点とする

カードの点数として、1 は 1 点と計算しても 11 点と計算してもよいが、手の点数が最大となる方を選ぶこととする

配られたカードの情報を入力とし、手の点数を出力するプログラムを作成してください。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットは以下の形式で与えられます。

c1 c2 ... cn

１行に i 番目のカードに書かれている整数 ci (1 ≤ ci ≤ 13) が空白区切りで１行に与えられます。カードの数 n は 100  を超えません。

データセットの数は 200 を超えません。

Output

データセットごとに手の点数を１行に出力します。

Sample Input

1
7 7 7
7 7 8
12 1
10 1 1
0

Output for the Sample Input

11
21
0
21
12



Sample input:
1
7 7 7
7 7 8
12 1
10 1 1
0


Sample output:
11
21
0
21
12

Submission status: Wrong Answer

Solution code:
s1 = range(11)+[10]*3
s2 = [0,11]+s1[2:]
def s(x,s):
  a = sum([s[e] for e in x])
  if a>21: a=0
  return a

while 1:
  x = map(int,raw_input().split())
  if x[0]==0: break
  print max(s(x, s1), s(x, s2))

Submission status: Wrong Answer

Solution code:
while 1:
  x = map(int, raw_input().split())
  if 0 in x: break
  s = sum([min(e, 10) for e in x])
  if s>21: s = 0
  for _ in [0]*x.count(1):
    if s+10>21: break
    s+=10
  print s

Submission status: Wrong Answer

Solution code:
while True:
    h = map(int, raw_input().split())
    if h[0] == 0:
        break
    t = 0
    a = 0
    for c in sorted(h, reverse=True):
        if 2 <= c <= 9:
            t += c
        elif 10 <= c:
            t += 10
        elif c == 1:
            a += 1
            
    if 21 < t:
        print 0
    else:
        t += a
        for i in range(a):
            if 21 < t + 10:
                print t
                break
            else:
                t += 10
        else:
            print t

Submission status: Wrong Answer

Solution code:
if input() == '1':
    pass

while True:
    inp = input()
    if inp == '0': break


    inp = inp.replace('11', '10')
    inp = inp.replace('12', '10')
    inp = inp.replace('13', '10')

    cards = tuple(map(int, inp.split()))
    ans = sum(cards)

    for i in range(cards.count(1)):
        if sum(cards) + 9 * (i+1) > 21:
            print('test', ans)
            break
        elif ans < sum(cards) + 9 * (i+1):
            print('test2', ans)
            ans = sum(cards) + 9 * (i+1)

    if ans > 21: ans = 0
    print(ans)

Submission status: Wrong Answer

Solution code:
if input() == '1':
    pass

while True:
    inp = input()
    if inp == '0': break


    inp = inp.replace('11', '10')
    inp = inp.replace('12', '10')
    inp = inp.replace('13', '10')

    cards = tuple(map(int, inp.split()))
    ans = sum(cards)

    for i in range(cards.count(1)):
        if sum(cards) + 10 * (i+1) > 21:
            print('test', ans)
            break
        elif ans < sum(cards) + 10 * (i+1):
            print('test2', ans)
            ans = sum(cards) + 10 * (i+1)

    if ans > 21: ans = 0
    print(ans)

Submission status: Wrong Answer

Solution code:
if input() == '1':
    pass

while True:
    inp = input()
    if inp == '0': break


    inp = inp.replace('11', '10')
    inp = inp.replace('12', '10')
    inp = inp.replace('13', '10')

    cards = tuple(map(int, inp.split()))
    ans = sum(cards)

    for i in range(cards.count(1)):
        if sum(cards) + 10 * (i+1) > 21:
            break
        elif ans < sum(cards) + 10 * (i+1):
            ans = sum(cards) + 10 * (i+1)

    if ans > 21: ans = 0
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

def point(lst):
  counter = Counter(lst)
  acc = 0
  for i in range(2, 10):
    acc += i * counter[i]
  for i in range(11, 14):
    acc += 10 * counter[i]
  one_num = counter[1]
  for i in range(one_num + 1):
    if i + (one_num - i) * 11 + acc <= 21:
      acc += i + (one_num - i) * 11
      break
  else:
    acc += one_num
  if acc > 21:
    return 0
  else:
    return acc

while True:
  s = input()
  if s == "0":
    break
  lst = list(map(int, s.split()))
  print(point(lst))

Submission status: Wrong Answer

Solution code:
while True:
    L = map(int,raw_input().split())
    if L == [0]:
        break
    a = L.count(0)
    s = sum(map(lambda x:min(10,x),L))
    for _ in xrange(a):
        if s <= 11:
            s += 11
    print s if s <= 21 else 0

Submission status: Wrong Answer

Solution code:
while True:
    L = map(int,raw_input().split())
    if L == [0]:
        break
    a = L.count(0)
    s = sum(map(lambda x:min(10,x),L))
    for _ in xrange(a):
        if s <= 11:
            s += 10
    print s if s <= 21 else 0

Submission status: Accepted

Solution code:
s1 = range(11)+[10]*3
def f(x):
  if x==[]: return [0]
  e = s1[x[0]]
  A1 = [e+e1 for e1 in f(x[1:])]
  A2 = []
  if e==1: A2 = [e+10 for e in A1]
  return filter(lambda x: x<22, A1 + A2)

while 1:
  x = map(int,raw_input().split())
  if x[0]==0: break
  a = f(x)
  print max(a) if a else 0

Submission status: Accepted

Solution code:
def f(x):
  if x==[]: return [0]
  e = min(x[0], 10)
  A = []
  for e1 in f(x[1:]):
    a = e+e1
    A += [a]
    if e==1: A += [a+10]
  return filter(lambda x: x<22, A)

while 1:
  x = map(int,raw_input().split())
  if x[0]==0: break
  a = f(x)
  print max(a) if a else 0

Submission status: Accepted

Solution code:
while 1:
  x = map(int, raw_input().split())
  if 0 in x: break
  s = sum([min(e, 10) for e in x])
  for _ in [0]*x.count(1):
    if s+10>21: break
    s+=10
  if s>21: s = 0
  print s

Submission status: Accepted

Solution code:
while True:
    h = map(int, raw_input().split())
    if h[0] == 0:
        break
    t = 0
    a = 0
    for c in sorted(h, reverse=True):
        if 2 <= c <= 9:
            t += c
        elif 10 <= c:
            t += 10
        elif c == 1:
            a += 1
            
    t += a
    if 21 < t:
        print 0
    else:
        for i in range(a):
            if 21 < t + 10:
                print t
                break
            else:
                t += 10
        else:
            print t

Submission status: Accepted

Solution code:
while True:
    inp = input()
    if inp == '0': break


    inp = inp.replace('11', '10')
    inp = inp.replace('12', '10')
    inp = inp.replace('13', '10')

    cards = tuple(map(int, inp.split()))
    ans = sum(cards)

    for i in range(cards.count(1)):
        if sum(cards) + 10 * (i+1) > 21:
            break
        elif ans < sum(cards) + 10 * (i+1):
            ans = sum(cards) + 10 * (i+1)

    if ans > 21: ans = 0
    print(ans)

Submission status: Accepted

Solution code:
while 1:
    a=list(map(int,input().split()))
    if a[0]==0:break
    b,c=0,0
    for i in a:
        if i>=10: b+=10
        else:b+=i
        if i==1:c+=1
    while c:
        if b+10<=21:b+=10
        c-=1
    print(0 if b>21 else b)

Submission status: Accepted

Solution code:
while 1:
    *C, = map(int, input().split())
    if C == [0]:
        break
    s = 0
    C.sort(reverse=1)
    one = 0
    for c in C:
        if c == 1:
            s += 1
            one += 1
        elif c > 10:
            s += 10
        else:
            s += c
    while one and s + 10 <= 21:
        s += 10
        one -= 1
    if s > 21:
        s = 0
    print(s)

Submission status: Accepted

Solution code:
# Aizu Problem 00169: Blackjack
#
import sys, math, os, bisect

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def blackjack(hand):
    hand = [min(h, 10) for h in hand]
    S = sum(hand)
    if S > 21:
        return 0
    C = hand.count(1)
    if C == 0:
        return S
    for k in range(C):
        C -= 1
        S += 10
        if S > 21:
            return S - 10
    return S

    
while True:
    hand = [int(_) for _ in input().split()]
    if len(hand) == 1 and hand[0] == 0:
        break
    print(blackjack(hand))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0169

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    while True:
        data = [int(x) for x in input().split()]
        if len(data) == 1 and data[0] == 0:
            break

        total = 0
        for d in data:
            if d > 10:
                total += 10
            else:
                total += d

        ace = data.count(1)
        for _ in range(ace):
            if total + 10 <= 21:
                total += 10

        if total > 21:
            total = 0

        print(total)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
while True:

    input_list = [int(item) for item in input().split(" ")]

    if input_list[0] == 0:
        break

    total = 0
    count = 0

    for num in input_list:
        if 10 < num:
            num = 10
        elif num == 1:
            count += 1

        total += num

        if 21 < total:
            break

    if total <= 21:

        while total + 10 <= 21 and 0 < count:
            total += 10
            count -= 1

        print(total)
    else:
        print(0)

Submission status: Accepted

Solution code:
def f(ans, n):
    if ans > 21:
        return 0
    if n == 0:
        if ans + 11 <= 21:
            return ans + 11
        elif ans + 1 > 21:
            return 0
        else:
            return ans + 1
    else:
        return max(f(ans+1, n-1), f(ans+11, n-1))



while True:
    S = input()
    if S == "0":
        break

    N = [int(i) for i in S.split()]
    N.sort()
    N.reverse()

    ans = 0
    for i in range(len(N)):
        if N[i] == 1:
            ans = f(ans, len(N)-i-1)
            break
        elif N[i] >= 10:
            ans += 10
        else:
            ans += N[i]
    if ans > 21:
        print(0)
    else:
        print(ans)

Submission status: Accepted

Solution code:
# AOJ 0169 Blackjack
# Python3 2018.6.19 bal4u

while True:
	cs = list(map(int, input().split()))
	if cs[0] == 0: break
	sum, ace = 0, 0
	for c in cs:
		if c == 1:
			c = 0
			ace += 1
		elif c >= 10: c = 10
		sum += c
		if sum + ace > 21: break
	if sum+ace > 21: print(0)
	elif ace > 0 and sum+ace <= 11: print(sum+ace+10)
	else: print(sum+ace)

Submission status: Accepted

Solution code:
from collections import Counter

def point(lst):
  counter = Counter(lst)
  acc = 0
  for i in range(2, 10):
    acc += i * counter[i]
  for i in range(10, 14):
    acc += 10 * counter[i]
  one_num = counter[1]
  for i in range(one_num + 1):
    if i + (one_num - i) * 11 + acc <= 21:
      acc += i + (one_num - i) * 11
      break
  else:
    acc += one_num
  if acc > 21:
    return 0
  else:
    return acc

while True:
  s = input()
  if s == "0":
    break
  lst = list(map(int, s.split()))
  print(point(lst))

Submission status: Accepted

Solution code:
def Judge(score,a,b):
    x,y=score+a,score+b
    if x<=21 and y<=21:
        return max(x,y)
    else:
        return min(x,y)

while True:
    L=sorted(map(int,raw_input().split()))[::-1]
    if L[0]==0:break
    score=0
    t=L.count(1)
    for i in L:
        if i>10:
            score+=10
        elif i>=2:
            score+=i
        elif t==1:
            score=Judge(score,1,11)
            break
        else:
            score=Judge(score,10+t,t)
            break
    if score>21:
        print 0
    else:
        print score

Submission status: Accepted

Solution code:
while True:
    cards = map(int, raw_input().split())
    if cards == [0]:
        break

    ace = 0
    sum = 0
    for i in cards:
        if i > 10:
            sum += 10
        else:
            sum += i
            if i == 1:
                ace += 1

    for i in range(ace):
        if sum + 10 <= 21:
            sum += 10

    print sum if sum <= 21 else 0

Submission status: Accepted

Solution code:
while True:
    cards = map(int, raw_input().split())
    if cards == [0]:
        break

    ace = 0
    sum = 0
    for i in cards:
        if i > 10:
            sum += 10
        else:
            sum += i
            if i == 1:
                ace += 1

    for i in range(ace):
        if sum <= 11:
            sum += 10

    print sum if sum <= 21 else 0

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement as cwr
while True:
    L = map(int,raw_input().split())
    if L == [0]:
        break
    c = L.count(1)
    s = sum(map(lambda a:min(10,a) if a!=1 else 0, L))
    if s + c > 21:
        print 0
    else:
        print min([sum(n)+s for n in cwr([1,11],c)],
                  key = lambda x:abs(21-x) if x <= 21 else x)

Submission status: Accepted

Solution code:
while True:
    L = map(int,raw_input().split())
    if L == [0]:
        break
    a = L.count(1)
    s = sum(map(lambda x:min(10,x),L))
    for _ in xrange(a):
        if s <= 11:
            s += 10
    print s if s <= 21 else 0

Submission status: Accepted

Solution code:
while 1:
	card=map(lambda x: min(int(x),10),raw_input().split())
	if card==[0]:break
	s=sum(card)
	for i in range(card.count(1)):
		if s+10>21:
			break
		s+=10
	print s if s<22 else 0

Submission status: Accepted

Solution code:
while True:
    ans = 0
    t = list(int(x) for x in input().split())
    if t[0] == 0:
        break
    for i in t:
        if i >= 11:
            ans += 10
        else:
            ans += i
    if 1 in t:
        if ans + 10 <= 21:
            ans += 10
    if ans > 21:
        ans = 0
    print(ans)

Submission status: Accepted

Solution code:
while True:
    n=[int(i) for i in input().split(" ")]
    if n==[0]:
        break
    for i in range(len(n)):
        if n[i]>10:
            n[i]=10
    one=n.count(1)
    score=sum(n)
    while score<=11 and one>0:
        if one>0:
            score+=10
            one-=1
    if score>21:
        score=0
    print(score)

Submission status: Accepted

Solution code:
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy
from test.support import _MemoryWatchdog

BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
MOD = 1000000007
EPS = 0.000000001
sys.setrecursionlimit(100000)


while True:

    table = list(map(int,input().split()))
    if len(table) == 1 and table[0] == 0:
        break

    num_1 = 0
    tmp_sum = 0
    for tmp in table:
        if tmp >= 11:
            tmp_sum += 10
        else:
            tmp_sum += tmp

        if tmp == 1:
            num_1 += 1

    if tmp_sum > 21:
        print(0)
    else:
        if num_1 == 0:
            print(tmp_sum)
        else:
            if tmp_sum <= 11:
                print(tmp_sum+10)
            else:
                print(tmp_sum)

Submission status: Accepted

Solution code:

while True:
    c = [int(x) for x in input().split()]
    if c[0] == 0:
        break
    L = []
    for i in c:
        if i > 10:
            L.append(10)
        else:
            L.append(i)

    p = []
    for  i in range(L.count(1)+1):
        t = sum(L) + 10 *i
        if t > 21:
            t =0
        p.append(t)

    print(max(p))

Submission status: Accepted

Solution code:
def solve():
    c = list(map(int, input().split()))
    c = [min(10, e) for e in c]
    
    if c == [0]:
        return 1
    if len(c) > 21:
        print(0)
        return 0
    else:
        ans = 0
        for bit in range(1 << len(c)):
            s = 0
            for i in range(len(c)):
                if c[i] != 1:
                    s += c[i]
                else:
                    if bit >> i & 1:
                        s += 11
                    else:
                        s += 1
            if s <= 21:
                ans = max(ans, s)
        print(ans)
        return 0

def main():
    while solve() == 0:
        pass

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
while 1:
    C = list(map(int,input().split()))
    if(C == [0]):
        break
    ans = [False]*22
    ans[0] = True
    for c in C:
        l = [min(c,10)]
        if(c == 1):
            l.append(11)
        ans_ = [False]*22
        for i in range(21,-1,-1):
            if not ans[i]:
                continue
            for a in l:
                if i+a > 21:
                    continue
                ans_[i+a] = True
        ans = ans_
    A = 0
    for i in range(22):
        if not ans[i]:
            continue
        A = max(A,i)
    print(A)

Submission status: Accepted

Solution code:
while True : 
    A = list(map(int,input().split()))
    if A[0] == 0 : break
    cnt = 0 
    S = 0 
    for i in A : 
        S += min(i, 10) 
        if i == 1 : cnt += 1
    while cnt > 0 and S + 10 <= 21 : 
        cnt -= 1
        S += 10
    if S > 21 :
        S = 0
    print(S)

Submission status: Accepted

Solution code:
while 1:
    hands = list(map(int, input().split()))
    if hands[0] == 0:
        break

    for i, hand in enumerate(hands):
        if hand > 10:
            hands[i] = 10

    if 1 in hands:
        cnt = hands.count(1)
        hands.sort(reverse=True)
        score = 0
        for hand in hands:
            if hand != 1:
                score += hand
            else:
                if score + cnt > 11:
                    score += 1
                    cnt -= 1
                else:
                    score += 11
                    cnt -= 1
    else:
        score = sum(hands)
    if score > 21:
        score = 0

    print(score)

Submission status: Accepted

Solution code:
while True:
    N = list(map(int, input().split()))
    if N[0] == 0: break
    total, ace = 0, 0
    for n in N:
        if n == 1:
            n = 0
            ace += 1
        elif n >= 10:
            n = 10
        total += n
        if total + ace > 21:
            break
    if total + ace > 21:
        print(0)
    elif 0 < ace and total + ace <= 11:
        print(total + ace + 10)
    else:
        print(total + ace)