Example

Input

R?????,2?)


Output

29
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

bool onlynum(int s, int e, string &str){
    bool ret = true;
    for(int i=s; i<=e; i++){
        if(str[i]=='L' || str[i]=='R' || str[i]=='(' || str[i]==')' || str[i]==','){
            ret = false;
            break;
        }
    }
    return ret;
}
string smax(string s, string t){
    if(s=="invalid") return t;
    if(t=="invalid") return s;
    if(s.length() == t.length()){
        return max(s, t);
    }else if(s.length() < t.length()){
        return t;
    }else{
        return s;
    }
}

int main(){
    string str, str2;
    cin >> str;
    int n = str.length();
    str2 = str;
    for(int i=0; i<n; i++){
        if(str2[i]=='?') str2[i]='9';
    }
    
    vector<vector<string> > dp(n, vector<string>(n, "invalid"));
    for(int j=0; j<n; j++){
        for(int i=0; i+j<n; i++){
            if(onlynum(i, i+j, str) && (j==0 || str[i]!='0')){
                dp[i][i+j] = str2.substr(i, j+1);
            }
            if(j>=5 && (str[i+1]=='(' || str[i+1]=='?') && (str[i+j]==')' || str[i+j]=='?')){
                for(int k=i+3; k<=i+j-2; k++){
                    if(str[k]!=',' && str[k]!='?') continue;
                    if(dp[i+2][k-1]=="invalid" || dp[k+1][i+j-1]=="invalid") continue;
                    if(str[i]=='L' || str[i]=='?'){
                        dp[i][i+j] = smax(dp[i][i+j], dp[i+2][k-1]);
                    }
                    if(str[i]=='R' || str[i]=='?'){
                        dp[i][i+j] = smax(dp[i][i+j], dp[k+1][i+j-1]);
                    }
                }
            }
        }
    }
    cout << dp[0][n-1] << endl;
    return 0;
}

