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 <string>
#include <vector>
#include <sstream>
using namespace std;

int main() {
    string s;
    while (getline(cin, s), s != "0") {
        stringstream ss(s);
        int n, sum = 0, one = 0;
        while (ss >> n) {
            sum += n;
            if (n == 1) ++ one;
        }
        int res = 0;
        for (int i=0; i<=one; ++i) {
            if (21 < sum + 10 * i) break;
            res = max(res, sum + 10 * i);
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>

using namespace std;

int main(){

  int n;
  char c;
  int sum = 0;
  vector<int> V;

  while(1){
    scanf("%d%c",&n,&c);
    if(n == 0) break;
    
    V.push_back(n);
    if(c == '\n'){
      int first = 0;
      for(int i = 0; i < V.size(); i++){
	if(V[i] > 10) V[i] = 10;

	if(V[i] != 1) sum += V[i];
	else first++;
      }

      if(sum+first*11 <= 21) sum += first*11;
      else sum += first;

      if(sum > 21) sum = 0;
    }else{
      continue;
    }
    cout << sum << endl;
    sum = 0;
    V.clear();
  }

  return 0;
}

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

using namespace std;

void solve()
{
	string str;
	while(getline(cin, str))
	{
		if(str[0] == '0')
		{
			return;
		}
		vector<int> Vec;
		string temp;
		int size = str.size();
		for(int i = 0; i < size; ++i)
		{
			if(str[i] == ' ')
			{
				int a;
				int multi = 1;
				int size2 = temp.size();
				for(int i = size2 - 1; i >= 0; --i)
				{
					a = (temp[i] - '0') * multi;
					multi *= 10;
				}
				Vec.push_back(a);
				temp.clear();
			}
			else
			{
				temp += str[i];
			}
		}
		int a;
		int multi = 1;
		int size2 = temp.size();
		for(int i = size2 - 1; i >= 0; --i)
		{
			a = (temp[i] - '0') * multi;
			multi *= 10;
		}
		Vec.push_back(a);
		temp.clear();
		
		int max;
		queue<int> Que;
		queue<int> NextQue;
		if(Vec[0] == 1)
		{
			Que.push(1);
			Que.push(11);
		}
		else if(10 <= Vec[0] && Vec[0] < 14)
		{
			Que.push(10);
		}
		else
		{
			Que.push(Vec[0]);
		}
		size = Vec.size();
		int pos = 1;
		while(pos != size)
		{
			while(!Que.empty())
			{
				int Front;
				if(Vec[pos] == 1)
				{
					Front = Que.front() + 1;
					NextQue.push(Front);
					Front = Que.front() + 11;
					NextQue.push(Front);
				}
				else if(10 <= Vec[pos] && Vec[pos] < 14)
				{
					Front = Que.front() + 10;
					NextQue.push(Front);
				}
				else
				{
					Front = Que.front() + Vec[pos];
					NextQue.push(Front);
				}
				Que.pop();
			}
			while(!NextQue.empty())
			{
				Que.push(NextQue.front());
				NextQue.pop();
			}
			pos++;
		}
		if(Que.size() == 1)
		{
			if(Que.front() > 21)
			{
				max = 0;
			}
			else
			{
				max = Que.front();
			}
		}
		else
		{
			vector<int> result;
			while(!Que.empty())
			{
				result.push_back(Que.front());
				Que.pop();
			}
			sort(result.begin(), result.end());
			max = result[0];
			size = result.size();
			for(int i = 0; i < size; ++i)
			{
				if(result[i] <= 21)
				{
					max = result[i];
				}
			}
		}
		cout << max << endl;
	}
}

int main()
{
	solve();
	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();
      for(i=0;i<si;i++){
	
	if(x==1){
	  k=v[i];
	  //v.push_back(k+11);
	  v[i]++;
	}else if(x>10){
	  v[i]+=10;
	}else {
	  v[i]+=x;
	}
	
      }
    }
    
    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;
}

C++: #define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

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

static const double eps = 1e-8;

vector<int> split(string delim,string str){
	vector<int> res;
	str += delim;
	for(int i=0;i<str.size();i++){
		for(int j=1;i+j<=str.size();j++){
			if(str.substr(i,j).find(delim) != string::npos){
				int num = atoi(str.substr(i,j-1).c_str());
				if(str.substr(i,j-1).size() >0 ) res.push_back(num);
				i+=j-1;
				break;
			}
		}
	}

	return res;
}

int main(){
	string str;
	while(getline(cin,str)){
		vector<int> data = split(" ",str);
		if(data.size()==1 && data[0]==0) break;
		set<int>* dp = new set<int>[data.size()+1]; 

		dp[0].insert(0);
		for(int i=0;i<data.size();i++){
			if(data[i] == 1){
				for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){
					if(*it+1 <= 21) dp[i+1].insert(*it+1);
					if(*it+11 <= 21) dp[i+1].insert(*it+11);
				}
			}
			else if(2 <= data[i] && data[i] <= 9){
				for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){
					if(*it+data[i] <= 21) dp[i+1].insert(*it+data[i]);
				}
			}
			else{
				for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){
					if(*it+10<= 21) dp[i+1].insert(*it+10);
				}
			}
		}

		int res=MAX_N;
		for(set<int>::iterator it = dp[data.size()].begin(); it != dp[data.size()].end(); it++){
			if(*it <= 21 && abs(*it-21) < abs(res-21)){
				res = * it;
			}
		}

		printf("%d\n",res == MAX_N ? 0 : res);

		delete[] dp;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<utility>
#include<cmath>
#include<sstream>
using namespace std;
#define all(a) a.begin(),a.end()


int main()
{
	string s;
	while(getline(cin,s))
	{
		if(s=="0")break;
		stringstream ss(s);
		int sum=0,c=0;
		int x;
		while(ss>>x)
		{
			sum+=min(10,x);
			if(x==1)c++;
		}
		while(sum+10<=21&&c)
		{
			c--;
			sum+=10;
		}
		if(sum>21)sum=0;
		
		cout<<sum<<endl;
	}
}

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));
        
        sort(all(in));
        reverse(all(in));
        
        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;
}