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 <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

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

using namespace std;
/** Problem0169 : Blackjack **/
int main()
{
	int ans, one, t;
	string s;
	
	while (!cin.eof()) {
		getline(cin, s);
		if (s == "0") break;
		stringstream ss(s);
		
		ans=0; one=0;
		while (!ss.eof()) {
			ss >> t;
			ans += t;
			if (t == 1) {
				one++;
				ans+=10;
			}
		}
		
		while (ans>21 && one>0) {
			one--;
			ans-=10;
		}
		
		if (ans > 21) ans=0;
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <sstream>

using namespace std;

int ten(vector<int> &cards,int index,int score){
    if(index == cards.size()){
        if(score >= 22) return 0;
        return score;
    }
    if(cards[index] >= 10) return ten(cards,index+1,score+10);
    if(cards[index] >= 2) return ten(cards,index+1,score+cards[index]);
    if(cards[index] == 1){
        return max(ten(cards,index+1,score+11),ten(cards,index+1,score+1));
    }
}

int main(){
    string s;
    while(getline(cin,s)){
        if(s=="0") break;
        stringstream ss;
        ss << s;
        vector<int> c;
        int temp;
        while(ss >> temp) c.push_back(temp);
        cout << ten(c,0,0) << endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TEMP_T T    cums(T& v){ T r(v.size()); partial_sum(v.begin(), v.end(), r.begin()); return r; }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    acum(vector<T>& v, T b=0){ return accumulate(v.begin(), v.end(), b); }
TEMP_T vector<T> inpt(int n){ vector<T> v(n); for (int i=0; i<n; i++) cin >> v[i]; return v; }

static inline int in(){ int x; scanf("%d", &x); return x; }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

struct Field {
  Field(int w, int h) : w(w), h(h) {}
  bool invalid(int x, int y){ return x < 0 || w <= x || y < 0 || h <= y; }
  const int w, h;
};

struct Solver {
  Solver(){}
  int input(){
    string line;
    getline(cin, line);
    if (line == "0") return 1;
    stringstream ss(line);
    string token;
    while (getline(ss, token, ' ')){
      stringstream is(token);
      int c;
      is >> c;
      card.push_back(c);
    }
    return 0;
  }
  int solve(){
    sort(card);
    int one_cnt = 0;
    int score = 0;
    for (int i = 0; i < card.size(); i++){
      if (card[i] == 1){
        one_cnt++;
        score += 11;
      }
      else {
        score += min(card[i], 10);
      }
      while (score > 21){
        if (one_cnt){
          score -= 10;
          one_cnt--;
        }
        else return 0;
      }
    }
    return score;
  }
  vector<int> card;
};

int main()
{
  while (1){
    Solver s;
    if (s.input()) break;
    cout << s.solve() << endl;
  }

  return (0);
}

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[100], p;

int dfs(int i, int n){
  if(n > 21) return 0;
  if(i == p) return n;

  if(a[i] == 1) return max(dfs(i+1, n+1), dfs(i+1, n+11));
  if(a[i] > 10) return dfs(i+1, n+10);
  return dfs(i+1, n+a[i]);
}

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

    //    rep(i, p) printf("%d ", a[i]); puts("");

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

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[128];
  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 <sstream>
using namespace std;
main()
{
    string s;
    while( getline( cin, s ), s != "0" )
    {
        stringstream ss(s);
        ss << s;
        int sum = 0, one = 0, a;
        while( ss >> a )
        {
            if( a == 1 ) one++;
            else sum += min( 10, a );
        }
        if( one > 21 || sum > 21 || one + sum > 21 )
        {
            cout << 0 << endl;
        }
        else
        {
            int p = ( 11 * one + sum - 21 + 10 - 1) / 10;
            cout << p + ( one - p ) * 11 + sum << endl;
        }
        
    }
    return 0;
}

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

using namespace std;

int main() {
	queue<int> q;
	string a;
	while (true) {
		getline(cin, a);
		if (a[0] == '0') {
			break;
		}
		int counter = 0;
		int b = 0;
		int sum = 0;
		for (int i = 0; i <= a.size(); i++) {
			if (a[i] >= '0' && a[i] <= '9') {
				b *= 10;
				b += a[i] - '0';
			} else {
				if (b > 10) {
					b = 10;
				} else if (b == 1) {
					counter++;
				}
				sum += b;
				b = 0;
			}
		}
		while (counter > 0) {
			if (sum + 10 <= 21) {
				sum += 10;
				counter--;
			} else {
				break;
			}
		}
		if (sum > 21) {
			sum = 0;
		}
		q.push(sum);
	}
	while (!q.empty()) {
		cout << q.front() << endl;
		q.pop();
	}
	return 0;
}