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 <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<VI> VVI;
template<class T> T gcd( T a, T b ) {
	return !b ? a : gcd( b, a % b );
}
template<class T> T lcm( T a, T b ) {
	return a / gcd( a, b ) * b;
}
template<class T> string print_v( vector<T> v ) {
	OSS oss;
	for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end();
          ++it_i ) {
		oss << *it_i << ", ";
	}
	return oss.str();
}

VI A;
int n;

int solve( int k, int d ) {
    if ( k >= n ) return d;
    if ( A[k] == 1 ) {
        return max( ( d + 1 > 21 ? 0 : solve( k + 1, d + 1 ) ), ( d + 11 > 21 ? 0 : solve( k + 1, d + 11 ) ) );
    } else if ( A[k] >= 10 ) {
        if ( d + 10 > 21 ) return 0;
        return solve( k + 1, d + 10 );
    } else {
        if ( d + A[k] > 21 ) return 0;
        return solve( k + 1, d + A[k] );
    }
    return 0;
}

int main() {
    string line;
    while ( getline( cin, line ) && line != "0" ) {
        A.clear();
        int w;
        ISS iss( line );
        while ( iss >> w ) A.push_back( w );
        n = A.size();
        cout << solve( 0, 0 ) << endl;
    }
	return 0;
}

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

int calc(int card[54], int num) {
  int sum = 0;
  int A = 0;
  int Ap[5][2] = {{0,0},{1,11},{2,12},{3,13},{4,14}};
  for(int i = 0; i < num; i++) {
    if(card[i] != 1) {
      if(card[i] >= 10) {
	sum += 10;
      } else {
	sum += card[i];
      }
    } else {
      A++;
    }
  }
  int best = -1;
  for(int i = 0; i < 2; i++) {
    int temp = sum + Ap[A][i];
    if(temp > 21) temp = 0;
    if(temp > best) best = temp;
  }

  return best;
}

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

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

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

vector<int> v;

int dp(int p, int n){
    if(p == v.size() && n <= 21) return n;
    else if(21 < n) return 0;
    else {
        if(v[p] == 1){
            return max(dp(p+1, n+1), dp(p+1, n+11));
        }
        else return dp(p+1, n+v[p]);
    }
}

int main(){
    string input;
    while(getline(cin, input), input != "0"){
        int n;
        v.clear();
        while(input.find_first_of(" ") != string::npos){
            n = atoi(input.substr(0, input.find_first_of(" ")).c_str());
            if(n/10 == 1) n = n-n%10;
            v.push_back(n);
            input = input.substr(input.find_first_of(" ")+1);
        }
        n = atoi(input.c_str());
        v.push_back(n);

        cout << dp(0, 0) << endl;
    }
}

C++: #include<stdio.h>
#include<iostream>
using namespace std;

int main(){
	char c;
int n,s=0;int A=0;
while(1){
	while(1){
scanf("%d%c",&n,&c);



	
if(n>1&&n<11)s+=n;
else if(n>10)s+=10;
else if(n==1){s+=1;A++;}
if(c=='\n')break;
}
if(s==0)break;
if(s>=22)printf("0\n");


else {
	if(A>0){
	for(int i=0;i<A;i++)
		if(s+10<=21)s+=10;
	
	}
	
	printf("%d\n",s);s=0;A=0;
}
}



return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdlib>

using namespace std;

int main(){

	string str;
	while(getline(cin,str)&&str!="0"){
		vector<int> v;
		int s=0;
		for(int i = 0; i < str.size(); i++){
			if(str[i]==' '||str.size()-1==i){
				int t;
				if(str.size()-1!=i)
					t= atoi(str.substr(s,i-s).c_str());
				else
					t= atoi(str.substr(s,i-s+1).c_str());
				v.push_back(t);
				s=i+1;
			}
		}
		sort(v.begin(),v.end(),greater<int>());
		int sum=0;
		for(int i = 0; i < v.size(); i++){
			if(v[i]>=10){
				sum+=10;
			}
			else if(v[i]<=9&&v[i]>=2){
				sum+=v[i];
			}
			else if(v[i]==1){
				int num = v.size()-i;
				for(int j = 0; j < num; j++){
					if((num-j-1)+sum+11<=21){
						sum+=11;
					}
					else
						sum+=1;
				}
				break;
			}
		}
		if(sum<=21)
			cout<<sum<<endl;
		else
			cout<<0<<endl;
	}

	return 0;
}

C++: #include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main()
{
	for(;;){
		int sum = 0;
		int ace = 0;
		bool finish = false;
		do{
			int a;
			scanf("%d", &a);
			if(a == 0){
				finish = true;
				break;
			}
			if(a == 1)
				++ace;
			else
				sum += min(a, 10);
			
		}while(getchar() == ' ');
		
		if(finish)
			break;
		
		int best = 0;
		rep(i, ace+1){
			int now = sum + i + (ace-i) * 11;
			if(now > 21)
				now = 0;
			best = max(best, now);
		}
		
		printf("%d\n", best);
	}
	
	return 0;
}

C++: #include <iostream>
#include <deque>
#include <string>
#include <sstream>

using namespace std;

int main(void){
		string str;

		while(getline(cin,str)){
			deque<int> q;
			int tmp;
			istringstream iss(str);
			iss >> tmp;
			if(tmp == 0) break;
			if(tmp == 1){
				q.push_back(1);
				q.push_back(11);
			}else if(tmp >= 10){
				q.push_back(10);
			}else{
				q.push_back(tmp);
			}

			while(iss >> tmp){
				int times = q.size();
				for(int i=0;i<times;i++){
					if(tmp == 1){
						if(q.front() + 1 <= 21){
							q.push_back(q.front() + 1);
						}
						if(q.front() + 11 <= 21){
							q.push_back(q.front() + 11);
						}
					}else if(tmp >= 10){
						if(q.front() + 10 <= 21){
							q.push_back(q.front() + 10);
						}
					}else{
						if(q.front() + tmp <= 21){
							q.push_back(q.front() + tmp);
						}
					}
					q.pop_front();
				}
			}

			int ret=0;
			while(!q.empty()){
				if(q.front() >= 22){
					//do nothing
				}else if(q.front() > ret){
					ret = q.front();
				}
				q.pop_front();
			}
			cout << ret << endl;
		}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  while(getline(cin,s),s!="0"){
    stringstream ss(s);
    int x=0,i,ans,k;
    int si=0;
    vector<int> v;
    v.clear();
    v.push_back(0);
    
    while(ss>>x){
      si=v.size();
      vector<int> n;
      for(i=0;i<si;i++){
	
	if(x==1){
	  k=v[i];
	  n.push_back(k+11);
	  v[i]++;
	}else if(x>10){
	  v[i]+=10;
	}else {
	  v[i]+=x;
	}
	
      }
      for(i=0;i<n.size();i++) v.push_back(n[i]);
      n.clear();
      
      sort(v.begin(),v.end());
      v.erase(unique(v.begin(),v.end()),v.end());
      v.erase(lower_bound(v.begin(),v.end(),22),v.end());
    }
    
    sort(v.begin(),v.end());
    ans=0;
    for(i=0;i<v.size();i++){
      if(v[i]<=21) ans=v[i];
    }
    cout << ans << endl;
    s="";
  }  
  return 0;
}