#include<iostream>
using namespace std;
int dp[101][22][22][22];
//dp[k][r][s][t]表示前k张card里面三堆分别为r点、s点、t点时的最优值
int MAX(int x,int y)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if( x>y )
return x;
else
return y;
}
int fun(char ch)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(ch>='2' && ch<='9')
return ch-'0';
if(ch == 'A')
return 1;
if(ch == 'F')
return 11;
return 10;
}
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int n,k,r,s,t,maxs,m;
while(cin>>n && n)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
char a;
for(k=1;k<=n;k++)
for(r=0;r<22;r++)
for(s=0;s<22;s++)
for(t=0;t<22;t++)
dp[k][r][s][t] = 0;
cin>>a;
m = fun(a);
if(m == 11)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[1][0][0][0] = 350;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[1][m][0][0] = 50;
dp[1][0][m][0] = 50;
dp[1][0][0][m] = 50;
}
for(k=2;k<=n;k++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cin>>a;
m = fun(a);
for(r=0;r<=21;r++)
for(s=0;s<=21;s++)
for(t=0;t<=21;t++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(dp[k-1][r][s][t] == 0)
continue;
if(r + m == 21 || (m == 11 && r < 21 ))//一定要放在前面
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ //r<21 表示还未超过21点
//只有能放的情况下才能操作,s,t可以等于21
dp[k][0][s][t] = MAX(dp[k][0][s][t],dp[k-1][r][s][t] + 150);
}
else if(r + m < 21)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[k][r+m][s][t] = MAX(dp[k][r+m][s][t],dp[k-1][r][s][t] + 50);
}
else if(r + m > 21 && r < 21)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[k][21][s][t] = MAX(dp[k][21][s][t],dp[k-1][r][s][t] + 50);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(s + m == 21 || (m == 11 && s < 21))//一定要放在前面
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[k][r][0][t] = MAX(dp[k][r][0][t],dp[k-1][r][s][t] + 250);
}
else if(s + m < 21)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[k][r][s+m][t] = MAX(dp[k][r][s+m][t],dp[k-1][r][s][t] + 50);
}
else if(s + m > 21 && s < 21)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[k][r][21][t] = MAX(dp[k][r][21][t],dp[k-1][r][s][t] + 50);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(t + m == 21 || (m == 11 && t < 21))//一定要放在前面
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[k][r][s][0] = MAX(dp[k][r][s][0],dp[k-1][r][s][t] + 350);
}
else if(t + m < 21)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[k][r][s][t+m] = MAX(dp[k][r][s][t+m],dp[k-1][r][s][t] + 50);
}
else if(t + m > 21 && t < 21)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp[k][r][s][21] = MAX(dp[k][r][s][21],dp[k-1][r][s][t] + 50);
}
}
}
maxs = 0;
for(k=1;k<=n;k++)
for(r=0;r<22;r++)
for(s=0;s<22;s++)
for(t=0;t<22;t++)
if(maxs < dp[k][r][s][t])
maxs = dp[k][r][s][t];
cout<<maxs<<endl;
}
return 0;
}