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<string>
using namespace std;
int main(){
	string a;
	while (getline(cin, a) && a!="0") {
		a += " ";
		int c = 0, sum = 0;
		string h = "";
		for (int i = 0; i < a.length(); i++) {
			if (a[i] == ' ') {
				int d = atoi(h.c_str());
				h = "";
				if (d == 1) c++;
				else {
					if (d == 10 || d == 11 || d == 12 || d == 13)
						sum += 10;
					else sum += d;
				}
			}
			else h += a[i];
		}
		if (sum > 21) {
			cout << 0 << endl;
			goto stop;
		}
		else {
			sum += c;
			if (sum > 21) {
				cout << 0 << endl;
				goto stop;
			}
			else {
				while ((21 - sum) >= 10&&c!=0) {
					sum += 10;
					c--;
				}
			}
		}
		cout << sum << endl;
	stop:;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int power(int n)
{
    int num=1;
    for(int i=0;i<n;i++)num*=10;
    return num;
}
int serch(int sum,int n)
{
    if(sum>21)return 0;
    else if(n==0)return sum;
    else return max(serch(sum+11,n-1),serch(sum+1,n-1));
}
int main()
{
    string str;
    while(getline(cin,str)){
        if(str[0]=='0')break;
        int card[1000]={},co=0,point=0;
        for(int i=0;;i++){
            if(str[i]==' '||str[i]=='\0'){
                for(int j=point;j<i;j++)card[co]+=(str[j]-'0')*power(i-1-j);
                co++;point=i+1;
            }
            if(str[i]=='\0')break;
        }
        int cou=0,sum=0;
        for(int i=0;i<co;i++){
            if(card[i]==1)cou++;
            else if(card[i]<10)sum+=card[i];
            else sum+=10;
        }
        cout<<serch(sum,cou)<<endl;
    }
}

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;
}

signed main() {
	bool a[50][50] = {};
	for (string s; getline(cin, s) && s != "0";) {
		fill(a[0], a[50], false);
		a[0][0] = true;
		vector<string> t = split(s, ' ');
		rep(i, 0, t.size()) {
			rep(j, 0, 50 - 11) {
				if (stoi(t[i]) <= 10)
					a[i + 1][stoi(t[i]) + j] |= a[i][j];
				if (stoi(t[i]) == 1)
					a[i + 1][11 + j] |= a[i][j];
				if (stoi(t[i]) >= 11)
					a[i + 1][10 + j] |= a[i][j];
			}
		}
		rrep(i, 0, 22) {
			if (a[t.size()][i]) {
				cout << i << endl;
				break;
			}
			if (i == 0)cout << 0 << endl;
		}
	}
	return 0;
}

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

int main()
{
  cin >> noskipws;
  while(true)
  {
    int cards[21];
    int card_num;
    int ace_num = 0;   //  エースがあるかどうか
    for (card_num = 0; ; card_num++)
    {
      if ( card_num == 21 )
      {
        int dummy;
        char ws;
        bool b = true;
        while (b)
        {
          cin >> dummy >> ws;
          if ( ws != ' ' )
          {
            b = false;
          }
        }
      }
      cin >> cards[card_num];
      if ( cards[card_num] == 0 ) return 0;
      if ( cards[card_num] == 1 ) ace_num++;
      char ws;
      cin >> ws;
      if ( ws != ' ' )
      {
        card_num++;
        break;
      }
    }

    int point = 0;
    for (int i=0; i<card_num; i++)
    {
      if ( cards[i] < 10 )
      {
        point += cards[i];
      }
      else
      {
        point += 10;
      }
    }

    while ( ace_num > 0 && point <= 11 )
    {
      point += 10;
      ace_num--;
    }
    if ( point > 21 )
    {
      point = 0;
    }

    cout << point << endl;

  }
}

C++: #define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 1e-8;

vector<int> split(string delim,string str){
	vector<int> res;
	str += delim;
	for(int i=0;i<str.size();i++){
		for(int j=1;i+j<=str.size();j++){
			if(str.substr(i,j).find(delim) != string::npos){
				int num = atoi(str.substr(i,j-1).c_str());
				if(str.substr(i,j-1).size() >0 ) res.push_back(num);
				i+=j-1;
				break;
			}
		}
	}

	return res;
}

int main(){
	string str;
	while(getline(cin,str)){
		vector<int> data = split(" ",str);
		if(data.size()==1 && data[0]==0) break;
		set<int>* dp = new set<int>[data.size()+1]; 

		dp[0].insert(0);
		for(int i=0;i<data.size();i++){
			if(data[i] == 1){
				for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){
					dp[i+1].insert(*it+1);
					dp[i+1].insert(*it+11);
				}
			}
			else if(2 <= data[i] && data[i] <= 9){
				for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){
					dp[i+1].insert(*it+data[i]);
				}
			}
			else{
				for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){
					dp[i+1].insert(*it+10);
				}
			}
		}

		int res=MAX_N;
		for(set<int>::iterator it = dp[data.size()].begin(); it != dp[data.size()].end(); it++){
			if(*it <= 21 && abs(*it-21) < abs(res-21)){
				res = * it;
			}
		}

		printf("%d\n",res == MAX_N ? 0 : res);

		delete[] dp;
	}
}

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

int main() {
  while( 1 ) {
    string line;
    getline( cin, line );
    vector<int> cards;
    istringstream iss(line);
    string item;
    while( iss >> item ) {
      cards.push_back( atoi( item.c_str() ) );
    }
    if( !cards[ 0 ] ) { return EXIT_SUCCESS; }
    sort( cards.begin(), cards.end() );
    int one_num = 0;
    for( ; cards[ one_num ] == 1; one_num++ ); // counts '1' cards
      
    int len = cards.size();
    int sum = 0;
    for( int i = 0; i < len; i++ ) {
      sum += min( cards[ i ], 10 );
    }
    sum = ( sum > 21 ? 0 : sum ); 
    while( sum != 0 && one_num > 0 ) {
      if( sum + 10 > 21 ) break;
      sum += 10;
      one_num--;
    }
    cout << sum << endl;
  }
}