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 <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(string s;getline(cin,s),s[0]!='0';){
		istringstream iss(s);
		vi a;
		for(int n;iss>>n;)
			a.push_back(n);
		
		int res=0;
		rep(i,a.size())
			res+=min(a[i],10);
		int onesize=count(allof(a),1);
		rep(i,onesize)
			if(res+10<=21)
				res+=10;
		if(res>21)
			res=0;
		
		cout<<res<<endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main(){
	string s;
	int tmp;
	while(getline(cin, s)) {
		if(s == "0") break;
		stringstream ss(s);
		vector<int> v;
		while(!ss.eof()) {
			ss >> tmp;
			v.push_back(tmp);
		}
		int ret = 0, cnt = 0;
		bool f = 0;
		rep(i, v.size()) {
			if(v[i] > 10) ret += 10;
			else if(v[i] == 1) cnt++; 
			else ret += v[i];
		}
		ret += cnt;
		while(cnt-- && ret < 12) {
			ret += 10;
		}
		if(ret >= 22) f = 1;
		if(f) cout << 0 << endl;
		else cout << ret << endl;
	}
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
	string base;
	int sum,tmp,count;
	
	
	while(getline(cin,base)){
		if(base[0]=='0')break;
		base+=' ';
		count=sum=tmp=0;
		for(int i=0;i<base.size();i++){
			if(base[i]==' '){
				if(tmp>10)tmp=10;
				if(tmp==1)count++,tmp+=10;
				sum+=tmp;
				tmp=0;
			}
			else{
				tmp*=10;
				tmp+=(base[i]-'0');
			}
		}
		
		while(sum>21 && count>0){
			sum-=10;
			count--;
		}
		if(sum>21)cout<<0<<endl;
		else cout<<sum<<endl;
	}
	
	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 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;
      }
    }
    return score;
  }
  vector<int> card;
};

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

  return (0);
}

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

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main(){
	string str;
	while(getline(cin,str)){
		if(str[0]=='0')break;
		stringstream ss;
		ss <<str;
		int x;
		int sum = 0;
		vector<int> data;
		int cnt = 0;
		while(ss>>x){
			if(x==1){
				cnt++;
			}
			if(x >=11){
				x = 10;
			}
			sum +=x;
			if(sum>21){
				sum = 0;
			}
		}
		for(int i =0;i < cnt;i++){
			if(sum+10<=21){
				sum +=10;
			}else{
				break;
			}
		}
		cout <<sum<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
	string s;
	while(getline(cin,s)&&s!="0"){
		s.push_back(' ');
		int one=0;
		int sum=0;
		
		string a;
		for(int i=0;i<s.size();i++){
			if(s[i]==' '){
				int b=stoi(a);
				if(b==1)one++;
				else if(b>10)sum+=10;
				else sum+=b;
				}
			else a.push_back(s[i]);
			}
			
		int dp[114][23]={};
		dp[0][max(22,sum)]=1;
		for(int i=1;i<=one;i++){
			for(int j=0;j<22;j++){
				dp[i][j+1]=dp[i][j];
				dp[i][min(22,j+11)]=dp[i][j];
				}
			}
			
		int ans=0;
		for(int i=0;i<22;i++){
			if(dp[one][i])ans=i;
			}		
		cout<<ans<<endl;	
		}
	return 0;
	}