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

int main() {
  string str;
  vector<int> card;
  while(getline(cin,str)) {
    if(str == "0") break;
    int score=0,ace=0;
    stringstream ss;
    int s;
    ss << str;
    while(ss >> s) {
      card.push_back(s);
    }
    ss.str("");
    ss.clear(stringstream::goodbit);



    for(unsigned int i=0;i<card.size();i++) {
      if(2 <= card[i] && card[i] <= 9) {
        score += card[i];
      }
      else if(10 <= card[i] && card[i] <= 13) {
        score += 10;
      }
      else { //card[i]==1
        ace++;
      }
    }

    if(score > 21) {
      cout << 0 << endl;
      card.clear();
      continue;
    }

    while(ace>0) {
      if(score + ace <= score + 11 + ace - 1 && score + 11 + ace - 1 <= 21) {
        score += 11;
      }
      else {
        score += 1;
      }
      ace--;
    }

    if(score > 21) {
      cout << 0 << endl;
    }
    else cout << score << endl;
    card.clear();
  }
}

C++: #include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
string s;
vector<int> parse(string str) {
	vector<int> ret; str += " "; int t = 0;
	for(int i = 0; i < str.size(); i++) {
		if(str[i] == ' ') ret.push_back(t), t = 0;
		else t = t * 10 + str[i] - 48;
	}
	return ret;
}
int main() {
	while(s = "", getline(cin, s)) {
		if(s == "0") break;
		vector<int> v = parse(s);
		int a = 0, b = 0;
		for(int i = 0; i < v.size(); i++) {
			if(v[i] > 10) a += 10;
			else a += v[i];
			if(v[i] == 1) b++;
		}
		cout << ((a < 22) ? a + min(b, (21 - a) / 10) * 10 : 0) << endl;
	}
}

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 <cstdio>
#include <cstdlib>

using namespace std;

typedef long long ll;

#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

int main()
{
    string s, t;
    int sum, ones;
    while(getline(cin, s) && s != "0") {
        s += " ";
        t = "";
        sum = ones = 0;
        for(int i = 0; i < s.size(); i++) {
            if(s[i] == ' ') {
                if(atoi(t.c_str()) == 1) {
                    ones++;
                } else {
                    if(atoi(t.c_str()) < 10) {
                        sum += atoi(t.c_str());
                    } else {
                        sum += 10;
                    }
                }
                t.clear();
            } else {
                t += s[i];
            }
        }
        sum += ones * 11;
        for(; ones-- && sum > 21;)
            sum -= 10;

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

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

main(){
  string str;
  while(getline(cin,str)){
    if(str[0]=='0')break;

    int ans=0,n1=0;
    stringstream ss(str);
    int t;
    while(ss>>t){
      if(t==1)n1++;
      if(t>10)t=10;
      ans+=t;
    }
    while(n1 && ans<12){
      n1--;
      ans+=10;
    }
    if(ans>21)ans=0;
    cout<<ans<<endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30

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

int a[14], p;

int main(){
  char str[514];
  while(fgets(str, sizeof(str), stdin) && str[0] != '0'){
    memset(a, 0, sizeof(a));
    char *tp;
    tp = strtok(str, " ");
    while(tp != NULL){
      a[atoi(tp)]++;
      tp = strtok(NULL, " ");
    }

    //    rep(i, p) printf("%d ", a[i]); puts("");
    int res = 0;
    a[10] += a[11]; a[10] += a[12]; a[10] += a[13];
    REP(i, 2, 11) res += a[i]*i;
    //    printf("%d\n", res);
    //    printf("%d\n", a[1]);

    int one = 11*a[1];
    while(res+one > 21 && one != a[1]){
      one -= 10;
    }
    //    printf("%d\n", res+one);
    res += one;
    if(res > 21) res = 0;

    printf("%d\n", res);
  }
  return 0;
}

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

using namespace std;

int main()
{
	char num[25];
	
	while (gets(num) != NULL){
		if (num[0] == '0'){
			break;
		}
		int score = 0;
		int f = 0;
		
		for (int i = 0; num[i] != '\0';){
			if (num[i] == '1' && (num[i + 1] >= '0' && num[i + 1] <= '3')){
				score += 10;
				i += 3;
			}
			else if (num[i] == '1'){
				score++;
				f = 1;
				i += 2;
			}
			else if ('2' <= num[i] && num[i] <= '9'){
				score += (num[i] - '0');
				i += 2;
			}
		}
		if (score > 21){
			score = 0;
		}
		else if (f == 1 && score + 10 <= 21){
			score += 10;
		}
		cout << score << endl;
	}
	
	return (0);
}