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<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<regex>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

//split delimiter??§?????????
vector<string> split(const string& input, char delimiter) {
	vector<string> ret;
	istringstream stream(input);
	string s;
	while (getline(stream, s, delimiter)) ret.push_back(s);
	return ret;
}
#define MAX 100
signed main() {
	bool a[MAX][50] = {};
	for (string s; getline(cin, s) && s != "0";) {
		fill(a[0], a[MAX], false);
		a[0][0] = true;
		vector<string> t = split(s, ' ');
		rep(i, 0, t.size()) {
			rep(j, 0, 30) {
				if (stoi(t[i]) <= 10)
					if (a[i][j])a[i + 1][stoi(t[i]) + j] = true;
				if (stoi(t[i]) >= 11)
					if (a[i][j])a[i + 1][10 + j] = true;
				if (stoi(t[i]) == 1)
					if (a[i][j])a[i + 1][11 + j] = true;
			}
		}
		rrep(i, 0, 22) {
			if (a[t.size()][i]) {
				cout << i << endl;
				break;
			}
			if (i == 0)cout << 0 << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

int main()
{
//	cut here before submit 
//	freopen("testcase.blackjack", "r", stdin);
	string str = "";
	vector < vector <int> > hand;
	vector <string> orig;

	while (getline (cin, str ) ){
		if (str.length() == 1 && str[0] == '0'){
			break;
		} // end if

		int n;
		stringstream ss(str);
		vector <int> card;

		orig.push_back(str );
		while (ss >> n){
			card.push_back (n );
		} // end while

		hand.push_back(card );

	} // end loop

	for (int i = 0; i < hand.size(); ++i){

		vector <int>::iterator it = hand[i].begin();
		int ace = 0;
		int sum = 0;

		for (; it != hand[i].end(); ++it ){
			if ((*it) == 1 ){
				++ace;
			}else if ((*it) >= 10){
				sum += 10;
			}else{
				sum += (*it);
			} // end if
		} // end for

		if (ace == 4){
			sum += 4;
		}else if (ace == 3){
			if (sum == 0){
				sum = 21;
			}else{
				sum += 3;
			} // end if
		}else if (ace == 2){
			if (sum <= 9){
				sum += 12;
			}else{
				sum += 2;
			} // end if
		}else if (ace == 1){
			if (sum <= 10){
				sum += 11;
			}else{
				sum += 1;
			} // end if
		} // end if
		
//		cout << orig[i] << ": ";
		if (sum <= 21){
 			cout << sum << endl;
		}else{
			cout << '0' << endl;
		} // end if

	} // end for

	return 0;
}

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 maxi = 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);
      }
      if (score > 21){
        if (one_cnt){
          score -= 10;
          one_cnt--;
        }
        else return 0;
      }
      maxi = max(maxi, score);
    }
    return maxi;
  }
  vector<int> card;
};

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

  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){
						q.push_back(q.front() + 1);
						q.push_back(q.front() + 11);
					}else if(tmp >= 10){
						q.push_back(q.front() + 10);
					}else{
						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<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(void){
	string s;
	while(getline(cin,s),s!="0"){
		int sum[2]={0};
		for(int i = 0 ; i < s.size() ; i +=2){
			if(s[i]!='1'){
//				cout<<"f1";
				sum[0]+=s[i]-'0';
				sum[1]+=s[i]-'0';
			}else{
				if(s[i+1]==' '|| s[i+1]=='\0'){
//					cout<<"f2";
					sum[0]++;
					sum[1]+=11;
				}else{
//					cout<<"f3";
					sum[0]+=10;
					sum[1]+=10;
					i++;
				}
			}
		}
		if(sum[1]<=21)cout<<sum[1]<<endl;
		else if(sum[0]<=21)cout<<sum[0]<<endl;
		else cout<<0<<endl;
	}
}