Example

Input

R?????,2?)


Output

29
#include<iostream>
#include<algorithm>
using namespace std;
string s;
string dp[55][55];
string mymax(string a,string b)
{
	if(a.size()==0||b.size()==0)return a.size()==0?b:a;
	if(a.size()<b.size())return b;
	else if(a.size()>b.size())return a;
	else return a<b?b:a;
}
int main()
{
	cin>>s;
	int N=s.size();
	for(int i=0;i<N;i++)
	{
		if(s[i]=='?')
		{
			dp[i][i+1]="9";
		}
		else if(s[i]>='0'&&s[i]<='9')dp[i][i+1]=s.substr(i,1);
		else dp[i][i+1]="";
	}
	for(int k=2;k<=N;k++)for(int i=0;i+k<=N;i++)
	{
		dp[i][i+k]="";
		bool flag=s[i]!='0';
		for(int j=0;j<k;j++)flag&=s[i+j]>='0'&&s[i+j]<='9'||s[i+j]=='?';
		if(flag)
		{
			for(int j=0;j<k;j++)dp[i][i+k]+=s[i+j]=='?'?'9':s[i+j];
			continue;
		}
		if(k<6)continue;
		if(!((s[i+1]=='('||s[i+1]=='?')&&(s[i+k-1]==')'||s[i+k-1]=='?')))continue;
		if(s[i]=='L'||s[i]=='?')
		{
			for(int l=3;l<k-2;l++)
			{
				if(s[i+l]!=','&&s[i+l]!='?')continue;
				if(dp[i+2][i+l]!=""&&dp[i+l+1][i+k-1]!="")dp[i][i+k]=mymax(dp[i][i+k],dp[i+2][i+l]);
			}
		}
		if(s[i]=='R'||s[i]=='?')
		{
			for(int l=3;l<k-2;l++)
			{
				if(s[i+l]!=','&&s[i+l]!='?')continue;
				if(dp[i+2][i+l]!=""&&dp[i+l+1][i+k-1]!="")dp[i][i+k]=mymax(dp[i][i+k],dp[i+l+1][i+k-1]);
			}
		}
	}
	cout<<(dp[0][N]!=""?dp[0][N]:"invalid")<<endl;
}

