Go: package main

import (
	"bufio"
		"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		cards := make([]int, len(buf))
		for i := range buf {
			cards[i], _ = strconv.Atoi(buf[i])
		}
		if cards[0] == 0 {
			break
		}
		sum, ace := 0, 0
		for _, v := range cards {
			if v == 1 {
				v = 0
				ace++
			} else if v >= 10 {
				v = 10
			}
			sum += v
			if sum+ace > 21 {
				break
			}
		}
		if sum+ace > 21 {
			fmt.Println(0)
		} else if ace > 0 && sum+ace<= 11 {
			fmt.Println(sum+ace+10) // 1->11なるAceは高々1枚
		} else {
			fmt.Println(sum+ace)
		}
	}
}

C++: #include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back


int main() {
    string s;
    while(getline(cin,s)){
        if(s=="0")break;
        
        vi in;
        string tmp="";
        rep(i,s.size()){
            if(s[i]!=' ')tmp+=s[i];
            else{
                int res=stoi(tmp);
                if(res>=10)res=10;
                in.pb(res);
                tmp="";
            }
        }
        in.pb(stoi(tmp));
        
        int len=in.size();
        int sum=0;
        int one=0;
        rep(i,len){
            if(in[i]==1)one++;
            sum+=in[i];
        }
        while(sum<=11 && one>0){
            one--;
            sum+=10;
        }
        if(sum>21)sum=0;
        cout<<sum<<endl;
    
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

int main(){
	string S;
	while(getline(cin,S)){
		priority_queue<int,vector<int>,greater<int> > pq[2]; pq[0].push(0);
		int cnt = 0;
		for(int i=0;i<S.size();i++){
			if(i==0 || S[i]==' '){
				int n = stoi(&S[i]);
				if(n==0) return 0;
				while(!pq[cnt&1].empty()){
					int pq_c = pq[cnt&1].top(); pq[cnt&1].pop();
					if(n==1) pq[(cnt+1)&1].push(pq_c+1), pq[(cnt+1)&1].push(pq_c+11);
					else if(n<10) pq[(cnt+1)&1].push(pq_c+n);
					else pq[(cnt+1)&1].push(pq_c+10);
				}
				cnt++;
			}
		}
		int ans = 0;
		while(!pq[cnt&1].empty()){
			if(pq[cnt&1].top()<=21) ans = max(ans,pq[cnt&1].top());
			pq[cnt&1].pop();
		}
		printf("%d\n",ans);
	}
	return 0;
}

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

int calc(int card[5], int num, int score=0, int cnt=0) {
  static int max;
  if(cnt == 0) max = -1;

  if(cnt == num) {
    if(score > 21) score = 0;
    if(score > max) max = score;
    return 0;
  }

  if(card[cnt] == 1) {
    calc(card, num, score+1, cnt+1);
    calc(card, num, score+11, cnt+1);
  } else if(card[cnt] > 10) {
    calc(card, num, score+10, cnt+1);
  } else {
    calc(card, num, score+card[cnt], cnt+1);
  }

  if(cnt == 0) return max;
}

int main() {
  while(1) {
    string str;
    getline(cin, str);
    if(str == "0") break;

    stringstream sin;
    sin << str;
    int card[5];
    fill(&card[0], &card[4]+1, 0);
    int num;
    for(int i = 0; i < 5; i++) {
      if(!(sin >> card[i])) {
	num = i;
	break;
      }
      if(i+1 == 5) {
	num = 5;
	break;
      }
    }
    cout << calc(card, num) << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
int main(){
	string s;
	int sum,ans,now,cou,pre;
	priority_queue<int> que;
	while(getline(cin,s),s[0]!='0'){
		sum=0;
		for(int i=0;i<s.size();i++){
			if(isdigit(s[i])){
					sum=sum*10+(s[i]-'0');
			}
			else{
				que.push(sum);
				sum=0;
			}
		}
		cou=0;
		que.push(sum);
		ans=0;
		while(!que.empty()){
			now=que.top();
			que.pop();
			if(now>=2&&now<=9)
				ans+=now;
			else if(now>9)
				ans+=10;
			else if(now==1)
				cou++;
		}
		int m=ans;
		for(int i=0;i<=cou;i++){
			pre=m+cou-i+i*11;
			if(pre<=21){
				ans=max(pre,ans);
			}
		}
		if(ans>21)
		cout<<'0'<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	string s;
	while (getline(cin, s), s != "0") {
		stringstream ss(s);
		int cnt = 0;
		int n;
		bool is_1 = false;
		while (ss >> n) {
			if (n == 1)is_1 = true;
			if (n < 10)cnt += n;
			else cnt += 10;
		}
		if (is_1&&cnt + 10 <= 21)cnt += 10;
		cout << (cnt > 21 ? 0 : cnt) << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={-1,0,1,0,1,1,-1,-1};
const int INF = 1<<30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define rep(i,j) reps(i,0,j)
#define MOD 1000000007
typedef pair<int,int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
    string str;
    while(getline(cin,str)){
        stringstream ss(str);
        int tmp;
        vi cards;
        while(ss >> tmp){
            cards.PB(tmp);
        }
        sort(cards.begin(),cards.end());
        if(cards[0] == 0)break;
        stack < int > S[2];
        S[0].push(0);
        rep(i,cards.size()){
            while(S[i%2].size()){
                int x = S[i%2].top();
                S[i%2].pop();
              //  cout << x << "\n";
                if(x > 21)x=0;
                if(cards[i] == 1){
                    S[1-i%2].push(x+1);
                    S[1-i%2].push(x+11);
                }
                else if(cards[i] > 9){
                    S[1-i%2].push(x+10);
                }
                else{
                    S[1-i%2].push(x+cards[i]);
                }
            }
        }
        int ans = 0;
        while(S[cards.size()%2].size()){
            int ret = S[cards.size()%2].top();
            S[cards.size()%2].pop();
            if(ret > 21){
                continue;
            }
            ans = max(ans,ret);
        }
        printf("%d\n",ans);
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<cstdio>
#include<functional>
#include<algorithm>
using namespace std;
int main(){
	while(true){
		int t;
		int one_num = 0;
		int ans = 0;
		vector< int > cards;
		
		scanf("%d", &t);
		if(t==0)
			break;
		cards.push_back( t );
		while( true ){
			char c;
			scanf("%c", &c);
			if( c == '\n' )
				break;
			else{
				scanf("%d", &t);
				cards.push_back( t );
			}
		}
		sort( cards.begin(), cards.end(), greater<int>() );
		for(unsigned int i = 0; i < cards.size(); ++i){
			one_num += cards[i] == 1 ? 1 : 0;
		}
		for(unsigned int i = 0; i < cards.size(); ++i){
			cards[i] = cards[i] > 10 ? 10 : cards[i];
			if( cards[i] == 1 ){
				int tt = one_num * 11;
				while( ans + tt > 21 ){
					tt -= 10;
				}
				if( tt < one_num )
					ans = 22;
				else
					ans = ans + tt;
				break;
			}else{
				ans += cards[i];
			}
		}
		printf("%d\n", ans > 21 ? 0 : ans);
	}
	return 0;
}

C++: #include<cstdio>
#include<vector>

using namespace std;

int main()
{
	for(int a;scanf("%d",&a),a;){
		vector<int> card;
		card.push_back(a);
		while(getchar()!='\n'){
			int b;
			scanf("%d",&b);
			card.push_back(b);
		}

		int cnt1=0,total=0;
		for(int i=0;i<card.size();i++){
			if(card[i]==1)	cnt1++;
			total+=card[i]>10?10:card[i];
		}

		for(int i=0;i<cnt1;i++,total+=10)
			if(total+10>21)	break;

		printf("%d\n",total>21?0:total);
	}

	return 0;
}