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 <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

vector<string> Split(char c,string s){
  vector<string> res(0);
  string str = "";
  rep(i,s.size()){
    if(s[i] == c){
      res.push_back(str);
      str.clear();
    }
    else{
      str += s[i];
    }
  }
  res.push_back(str);
  return res;
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  while(getline(cin,s)){
    if(s[0] == '0') break;
    auto ss = Split(' ',s);
    vector<int> v(ss.size());
    int one = 0;
    rep(i,v.size()){
      v[i] = ston(ss[i],1);
      v[i] = (v[i]>10)?10:v[i];
      if(v[i] == 1){
        ++one;
      }
    }
    sort(all(v),greater<int>());
    int sc = 0;
    bool f = true;
    rep(i,v.size()){
      if(v[i] != 1){
        sc += v[i];
        if(sc > 21){
          sc = 0;
          f = false;
          cout << sc << endl;
          break;
        }
      }
    }
    if(f){
      while(one){
        if(sc + 11 < 22){
          sc += 11;
          --one;
        }
        else{
          sc += 1;
          --one;
        }
      }

      if(sc > 21){
        sc = 0;
      }
      cout << sc << endl;
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int check(int n){
  if(n > 21) return 0;
  return n;
}
int main(void){
  int n,sum=0,f=0;
  while(cin >> n && n){
    if(n>10) n = 10;
    if(n==1) f = 10;
    sum += n;
    if(getchar() == '\n'){
      cout << max(check(sum),check(sum+f)) << endl;
      sum = 0;
      f = 0;
    }
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
//#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;


signed main() {
	vi d;
	while(1){
		int a;
		char c;
		scanf("%d%c",&a,&c);
		if(a == 0) break;
		d.push_back(a);
		if(c == '\n'){
			int sum = 0;
			int f = 0;
			rep(i, 0, d.size()){
				//cout <<d[i]<<" ";
				if(d[i] == 1) f ++;
				else sum += min(10, d[i]);
			}
			if(f){
				if(21 - sum - f + 1 >= 11) sum += 11 + f - 1;
				else sum += f;
			}
			cout<< (sum > 21 ? 0 : sum) << endl;
			d.clear();
		}
	}
}

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 pair<int,Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
	string str;
	while(true){
		getline(cin,str);
		vi num;
		stringstream ss(str);
		int tmp;
		while(ss >> tmp){
			num.PB(tmp);
		}
		if(num[0] == 0)break;
		int one = 0;
		int ans = 0;
		rep(i,num.size()){
			if(num[i]==1){
				ans++;
				one++;
			}
			else{
				if(num[i] > 9){
					ans+=10;
				}
				else{
					ans+=num[i];
				}
			}
		}
		while(one--){
			if(ans+10 < 22){
				ans += 10;
			}
		}
		if(ans >= 22)ans = 0;
		printf("%d\n",ans);
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

int rec(int sum, int one)
{
    if (sum > 21)
        return -1;
    else if (one == 0 && sum > 21)
        return -1;
    else if (one == 0 && sum <= 21)
        return sum;

    int res = rec(sum + 1, one - 1);
    res = max(res, rec(sum + 11, one - 1));

    return res;
}

bool solve(string &s)
{
    int sum = 0, one = 0;
    int index = 0;

    s += " ";
    while (index < s.size()) {
        int num = 0;

        while (index < s.size()) {
            if ('0' <= s[index] && s[index] <= '9') {
                num *= 10;
                num += (int)(s[index] - '0');
                index++;
            }
            else
                break;
        }

        if (num == 1)
            one++;
        else if (num / 10 == 1)
            sum += 10;
        else
            sum += num;
        index++;
    }

    if (sum == 0 && one == 0)
        return false;

    if (sum > 21) {
        cout << 0 << endl;
        return true;
    }
    else if (one == 0) {
        cout << sum << endl;
        return true;
    }
    else {

        int ans = rec(sum, one);
        if (ans >= 0)
            cout << ans << endl;
        else
            cout << 0 << endl;
        return true;
    }
}

int main()
{
    string s;

    while (true) {
        getline(cin, s);
        if (!solve(s))
            break;
    }

    return 0;
}

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

int main() {
  string s;
  while(getline(cin, s)) {
    if(s[0] == '0') break;
    vector<int> card;
    int one = 0;
    for(int i = 0; i < s.size(); i++) {
      if('0' <= s[i] && s[i] <= '9') {
        if(i+1 != s.size() && '0' <= s[i+1] && s[i+1] <= '3') {
          card.push_back((s[i]-'0')*10 + (s[i+1]-'0'));
          i++;
        } else {
          card.push_back(s[i]-'0');
        }
        if(i > 0 && s[i-1] == ' ' && s[i] == '1') one++;
        else if(i == 0 && s[i+1] == ' ' && s[i] == '1') one++;
      }
    }
    sort(card.begin(), card.end(), greater<int>());

    int ans = 0;
    for(int i = 0; i < card.size(); i++) {
      if(card[i] != 1) {
        if(card[i] > 10) ans += 10;
        else ans += card[i];
      } else {
        if(ans+10+one <= 21) ans += 11;
        else ans += 1;
        one--;
      }
    }
    if(ans > 21) ans = 0;
    cout << ans << endl;
  }
}