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

int Parse(string a) {
	int s = 0;
	for (int b = 0; b < a.length(); b++) {
		s *= 10;
		s += a[b] - 48;
	}
	return s;
}
int main() {
	string a;
	while (1) {
		getline(cin, a);
		if (a[0]=='0')break;
		vector<int>s;
		string c="";
		for (int b = 0; b < a.length(); b++) {
			if (a[b] == ' ') { s.push_back(Parse(c)); c = ""; }
			else c += a[b];
		}
		s.push_back(Parse(c));
		int m = 0;
		int n = 0;
		for (int d = 0; d < s.size(); d++) {
			if (s[d] > 9&&s[d]<14)m += 10;
			else if (s[d] > 1&&s[d]<10)m += s[d];
			else n++;
			if (m > 21) { cout << 0 << endl; goto l; }
		}
		if (m + 10 + n < 22)m += 10 + n;
		else m += n;
		if (m > 21)m = 0;
		cout << m << endl;
	l:;
	}
}

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

int calc(int card[], int num) {
  int sum = 0;
  int A = 0;
  int Ap[12][2] = {{0,0},{1,11},{2,12},{3,13},{4,14},{5,15},{6,16},{7,17},{8,18},{9,19},{10,20},{11,21}};
  for(int i = 0; i < num; i++) {
    if(card[i] != 1) {
      if(card[i] >= 10) {
	sum += 10;
      } else {
	sum += card[i];
      }
    } else {
      A++;
    }
  }
  int best = -1;
  if(A >= 12) {
    sum += A;
    A = 0;
  }
  for(int i = 0; i < 2; i++) {
    int temp = sum + Ap[A][i];
    if(temp > 21) temp = 0;
    if(temp > best) best = temp;
  }

  return best;
}

int main() {
  while(1) {
    string str;
    getline(cin, str);
    if(str == "0") break;

    stringstream sin;
    sin << str;
    int card[1000];
    fill(&card[0], &card[999]+1, 0);
    int num;
    for(int i = 0; i < 1000; i++) {
      if(!(sin >> card[i])) {
	num = i;
	break;
      }
      if(i+1 == 1000) {
	num = 1000;
	break;
      }
    }
    cout << calc(card, num) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main(){
    string str;
    
    while(getline(cin,str),str!="0"){
        int sum = 0,num,cnt=0;
        stringstream s;
        s << str;
        
        while(s >> num){
            if(2 <= num && num <= 9) sum += num;
            else if(num >= 10) sum += 10;
            else{
                sum++;
                cnt++;
            }
        }
        if(sum <= 11 && cnt){
            sum += 10;
            cnt--;
        }
        if(sum > 21) cout << '0' << endl;
        else cout << sum << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
typedef pair<int, string> PSI;

string s;

int stringtoi(string s){
	int res;
	stringstream ss;
	ss << s;
	ss >> res;
	return res;
}

template <typename List> 
void split(const string s, const string delim, List& result){
	result.clear();

	string::size_type pos = 0;
	while (pos != string::npos){
		string::size_type p = s.find(delim, pos);

		if (p == -1){
			result.push_back(stringtoi(s.substr(pos)));
			break;
		}
		else{
			result.push_back(stringtoi(s.substr(pos, p - pos)));
		}

		pos = p + delim.size();
	}
}

int main(){
	while (getline(cin, s) && s != "0"){
		vector<int> vi;
		int cnt = 0, sum = 0;
		split(s, " ", vi);
		REP(i, vi.size()){
			if (vi[i] == 1)cnt++;
			if (vi[i] >= 10)sum += 10;
			else sum += vi[i];
		}
		while (sum <= 11 && cnt > 0){
			cnt--;
			sum += 10;
		}
		if (sum <= 21)cout << sum << endl;
		else cout << 0 << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

string s;

int changei(string buf){
	int res;
	stringstream ss;
	ss << buf;
	ss >> res;
	return res;
}

int main(){
	while (getline(cin, s)){
		if (s == "0")break;
		stringstream ss;
		string buf;
		ss << s;
		int sum = 0;
		int co = 0;
		while (ss >> buf){
			int a = changei(buf);
			if (a > 10)a = 10;
			if (a == 1){
				co++;
			}
			else{
				sum += a;
			}
		}
		int k = sum;
		REP(i, co + 1){
			int a = k + 1 * i + 11 * (co - i);
			if (a <= 21)sum = max(a, sum);
		}
		if (sum > 21)cout << 0 << endl;
		else cout << sum << 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[128];
  while(fgets(str, sizeof(str), stdin) && str[0] != '0'){
    str[strlen(str)] = 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;
}