Problem:
D - LR
Problem Statement
JAG Kingdom will hold a contest called ICPC (Interesting Contest for Producing Calculation).
At the contest, you are given a string composed of
?
s and
usable characters
. You should replace all
?
s in the string with the usable characters to make the string valid mathematical expression, before submitting it. The usable characters are
L
,
R
,
(
,
)
,
,
,
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
, and
9
.
For example, suppose that you are given the string "R(??3,??1?78??1?)?", then you can submit "R(123,L(1678,213))" as an example.
The submitted string will be scored as follows.
Let $L$ and $R$ be functions defined by $L(x,y)=x$, $R(x,y)=y$, where $x$ and $y$ are non-negative integers.
The submitted string will be regarded as a mathematical expression, whose value will be the score. For example, the score of the string "R(123,L(1678,213))" is $R(123,L(1678,213)) = R(123,1678) = 1678$.
If the string cannot be evaluated as a mathematical expression about the functions $L$ and $R$, the string will be rejected. For example, "R", "R(3)", "R(3,2", "R(3,2,4)" and "LR(3,2)" are all invalid.
And strings that contain numbers with extra leading zeros, will be rejected. For example, "R(04,18)" is invalid, while "R(0,18)" is valid.
The winner of the contest will be the person getting the highest score. Your friend Jagger, who is going to join the contest, wants to be the winner. You are asked by Jagger to make the program finding the possible highest score for the input string.
Input
The input contains one string in a line, whose length $N$ is between $1$ and $50$, inclusive.
You can assume that each element in the string is one of
L
,
R
,
(
,
)
,
,
,
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
, or
?
.
Output
Display the possible highest score in a line for the given string.
If it's impossible to get valid strings for the input string, print "invalid" in a line.
Sample Input 1
R?????,2?)
Output for the Sample Input 1
29
Sample Input 2
???3??
Output for the Sample Input 2
999399
Sample Input 3
????,??,???
Output for the Sample Input 3
invalid
Sample Input 4
?????,??,???
Output for the Sample Input 4
99
Sample Input 5
L(1111111111111111111111111111111111111111111,2)
Output for the Sample Input 5
1111111111111111111111111111111111111111111
Sample Input 6
L?1???????????????????????????????????????????????
Output for the Sample Input 6
199999999999999999999999999999999999999999999
Sample Input 7
L?0???????????????????????????????????????????????
Output for the Sample Input 7
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define inc(i, a, b) for (int i = (a); i <= (b); ++i)
#define dec(i, a, b) for (int i = (a); i >= (b); --i)

string mx(string a, string b) {
    if (a == "invalid") return b;
    if (b == "invalid") return a;
    if (a.size() > b.size()) return a;
    if (a.size() < b.size()) return b;
    return max(a, b);
}

string solve(string s) {
    if (s == "") return "invalid";
    bool f = false;
    for (auto c : "LR(,)")  if (find(s.begin(), s.end(), c) != s.end()) f = true;

    if (f) {
        if (s.size() < 6) return "invalid";
        if (s[0] != 'L' && s[0] != 'R' && s[0] != '?') return "invalid";
        if (s[1] != '(' && s[1] != '?') return "invalid";
        if (s.back() != ')' && s.back() != '?') return "invalid";
        string m = "invalid";
        inc(i, 3, s.size() - 3) {
            if (s[i] != ',' && s[i] != '?') continue;
            string l = solve(s.substr(2, i - 2));
            string r = solve(s.substr(i + 1, s.size() - 2 - i));
            if (s[0] != 'L' && l != "invalid") m = mx(m, r);
            if (s[0] != 'R' && r != "invalid") m = mx(m, l);
        }
        return m;
    } else {
        if (s[0] == '0' && s.size() != 1) return "invalid";
        string t;
        rep(i, s.size()) t += s[i] == '?' ? '9' : s[i];
        return t;
    }
}


int main() {
    string s;
    cin >> s;
    cout << solve(s) << endl;
}
accuracy: 1
time_ms: 550
memory_kb: 3444
final_rank: 10

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < (n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros

int N;
string s;
string memo[51][51];

string MAX(const string& a, const string& b) {
    if(a.size() != b.size()) {
        return (a.size() < b.size() ? b : a);
    }
    REP(i, a.size()) if(a[i] != b[i]) {
        return (a[i] < b[i] ? b : a);
    }
    return a;
}

bool eq(char l, char r) {
    if(l == '?') return true;
    return l == r;
}

string dp(int l, int r) {
    if(memo[l][r] != "INF") return memo[l][r];
    if(l >= r) {
        memo[l][r] = "";
        return memo[l][r];
    }
    string res = "";
    {
        bool ok = true;
        string t = "";
        REP(i, l, r) {
            if(isdigit(s[i]) or s[i] == '?') { 
                if(isdigit(s[i])) t += s[i];
                else t += '9';
            } else {
                ok = false; break;
            }
        }
        if(t.front() == '0' and (int)t.size() >= 2) ok = false;
        if(ok) res = MAX(res, t);
    }
    if((eq(s[l], 'L') or eq(s[l], 'R')) and r-l >= 6 and eq(s[l+1], '(') and eq(s[r-1], ')')) {
        REP(i, l+3, r-2) { // ','の位置
            if(!eq(s[i], ',')) continue; 
            string L = dp(l+2, i);
            string R = dp(i+1, r-1);
            if(L == "" or R == "") continue;
            if(eq(s[l], 'L')) res = MAX(res, L);
            if(eq(s[l], 'R')) res = MAX(res, R);
        }
    }
    // debug(l, r, res);
    return res;
}

int main() {
    cin >> s;
    N = (int)s.size();
    REP(i, 51) REP(j, 51) memo[i][j] = "INF";
    auto ans = dp(0, N);
    if(ans == "") ans = "invalid";
    cout << ans << endl;
}
accuracy: 1
time_ms: 1710
memory_kb: 3588
final_rank: 17

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

const string e="invalid";

string compare(string s,string t){
    if (t==e) return s;
    if (s==e) return t;
    if (s.size()>t.size()) return s;
    if (t.size()>s.size()) return t;
    for (int i=0;i<s.size();++i){
        if (s[i]>t[i]) return s;
        if (t[i]>s[i]) return t;
    }
    return s;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S; cin >> S;

    auto dfs=[&](auto self,int l,int r)->string{
        string num="";
        for (int i=l;i<r;++i){
            if (S[i]!='?'&&!isdigit(S[i])) break;
            if (i==l&&r-l>1&&S[i]=='0') break;
            num+=(S[i]=='?'?'9':S[i]);
            if (i==r-1) return num;
        }

        if (r-l<6) return e;
        if (S[l]!='?'&&S[l]!='L'&&S[l]!='R') return e;
        if (S[l+1]!='?'&&S[l+1]!='(') return e;
        if (S[r-1]!='?'&&S[r-1]!=')') return e;
        string res=e;
        for (int i=l+3;i<r-2;++i){
            if (S[i]!='?'&&S[i]!=',') continue;
            string L=self(self,l+2,i),R=self(self,i+1,r-1);
            if (L==e||R==e) continue;
            if (S[l]=='L') res=compare(res,L);
            else if (S[l]=='R') res=compare(res,R);
            else res=compare(res,compare(L,R));
        }
        return res;
    };

    string ans=dfs(dfs,0,S.size());

    cout << ans << '\n';
}
accuracy: 1
time_ms: 830
memory_kb: 3244
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD=1000000007;
// const long long MOD=998244353;
#define LOCAL
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(),(x).end()
const int INF=1e9;
const long long IINF=1e18;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const char dir[4]={'D','R','U','L'};

template<typename T>
istream &operator>>(istream &is,vector<T> &v){
    for (T &x:v) is >> x;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U> &p){
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template<typename T,typename U,typename V>
ostream&operator<<(ostream &os,const tuple<T,U,V> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ')';
    return os;
}
template<typename T,typename U,typename V,typename W>
ostream&operator<<(ostream &os,const tuple<T,U,V,W> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ',' << get<3>(t) << ')';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const unordered_map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const multiset<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const unordered_set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const deque<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}

void debug_out(){cerr << '\n';}
template<class Head,class... Tail>
void debug_out(Head&& head,Tail&&... tail){
    cerr << head;
    if (sizeof...(Tail)>0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...) cerr << " ";\
cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\
cerr << " ";\
debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}
template<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}

template<class T1,class T2> inline bool chmin(T1 &a,T2 b){
    if (a>b){a=b; return true;} return false;
}
template<class T1,class T2> inline bool chmax(T1 &a,T2 b){
    if (a<b){a=b; return true;} return false;
}
#pragma endregion


// 数字は使うなら9一択
// RとLに対して帳尻が合うように挿入する
// dfs(l,r)=[l,r)から構成される最大値を求める(主にinvalidの判定)

const string e="invalid";

string compare(string s,string t){
    if (t==e) return s;
    if (s==e) return t;
    if (s.size()>t.size()) return s;
    if (t.size()>s.size()) return t;
    for (int i=0;i<s.size();++i){
        if (s[i]>t[i]) return s;
        if (t[i]>s[i]) return t;
    }
    return s;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S; cin >> S;

    auto dfs=[&](auto self,int l,int r)->string{
        // 全部数字
        string num="";
        for (int i=l;i<r;++i){
            if (S[i]!='?'&&!isdigit(S[i])) break;
            if (i==l&&r-l>1&&S[i]=='0') break;
            num+=(S[i]=='?'?'9':S[i]);
            // if (i==r-1){debug(l,r,num);}
            if (i==r-1) return num;
        }

        // L(...) or R(...)
        if (r-l<6) return e;
        if (S[l]!='?'&&S[l]!='L'&&S[l]!='R') return e;
        if (S[l+1]!='?'&&S[l+1]!='(') return e;
        if (S[r-1]!='?'&&S[r-1]!=')') return e;
        string res=e;
        for (int i=l+3;i<r-2;++i){
            if (S[i]!='?'&&S[i]!=',') continue;
            string L=self(self,l+2,i),R=self(self,i+1,r-1);
            // if (i==0){debug(L,i,R);}
            if (L==e||R==e) continue;
            if (S[l]=='L') res=compare(res,L);
            else if (S[l]=='R') res=compare(res,R);
            else res=compare(res,compare(L,R));
        }
        // debug(l,r,res);
        return res;
    };

    string ans=dfs(dfs,0,S.size());

    cout << ans << '\n';
}
accuracy: 1
time_ms: 830
memory_kb: 3240
final_rank: 12

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

string num[60][60];
string dp[60][60];
string s;

string MAX(string a, string b) {
	if (a == "invalid")return b;
	if (b == "invalid")return a;
	if (a.size() > b.size())return a;
	if (a.size() < b.size())return b;
	return max(a, b);
}

string func(int l, int r) {
	if (num[l][r] != "") {
		return num[l][r];
	}
	if (l < r&&s[l] == '0') {
		return num[l][r] = "invalid";
	}
	for (int i = l; i <= r; i++) {
		if ('0' <= s[i] && s[i] <= '9') {
			num[l][r].push_back(s[i]);
		}
		else if (s[i] == '?') {
			num[l][r].push_back('9');
		}
		else {
			num[l][r] = "invalid";
			return num[l][r];
		}
	}
	return num[l][r];
}

string ret(int l, int r) {
	dp[l][r] = max(dp[l][r], func(l, r));
	if (dp[l][r] != "invalid")return dp[l][r];
	if (s[l + 1] != '('&&s[l + 1] != '?')return dp[l][r];
	if(s[r] != ')'&&s[r] != '?')return dp[l][r];
	for (int i = l + 3; i <= r - 2; i++) {
		if (s[i] != ','&&s[i] != '?')continue;
		string lst = ret(l + 2, i - 1);
		string rst = ret(i + 1, r - 1);
		if (lst == "invalid" || rst == "invalid")continue;
		if (s[l] == 'R' || s[l] == '?')dp[l][r] = MAX(dp[l][r], rst);
		if (s[l] == 'L' || s[l] == '?')dp[l][r] = MAX(dp[l][r], lst);
	}
	return dp[l][r];
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> s;
	cout << ret(0, s.size() - 1) << endl;
}
accuracy: 1
time_ms: 830
memory_kb: 3368
final_rank: 14

code_snippet: #include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>


template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
constexpr long long mod = 998244353LL;

using namespace std;
typedef long long ll;

string s;
vector<vector<string>> dp;

bool AllNumber(int lf, int rg) {
	bool ok = true;
	for (int i = lf; i < rg; i++) if (s[i] != '?' and !isdigit(s[i])) { ok = false; break; }
	if (rg - lf > 1 and s[lf] == '0') ok = false;
	return ok;
}

string smax(string &a, string b) {
	if (a == "!") return b;
	if (a.size() > b.size()) return a;
	else if (a.size() < b.size()) return b;
	else {
		for (int i = 0; i < a.size(); i++) {
			if (a[i] > b[i]) return a;
			if (a[i] < b[i]) return b;
		}
	}
	return a;
}
string dfs(int lf, int rg) {
	if (dp[lf][rg] != "") return dp[lf][rg];
	if (rg - lf > 1 and s[lf] == '0') return "!";
	if (AllNumber(lf, rg)) {
		string res; res.reserve(rg - lf);
		for (int i = lf; i < rg; i++) {
			if (s[i] == '?') res += '9';
			else res += s[i];
		}
		return res;
	}
	if (rg - lf < 6) return "!";
	if (s[lf] != 'R' and s[lf] != 'L' and s[lf] != '?') return "!";
	if (s[lf + 1] != '(' and s[lf + 1] != '?') return "!";
	if (s[rg - 1] != ')' and s[rg - 1] != '?') return "!";
	string res = "!";
	for (int i = lf + 3; i < rg - 2; i++) {
		if (s[i] != '?' and s[i] != ',') continue;
		string ls = move(dfs(lf + 2, i));
		string rs = move(dfs(i + 1, rg - 1));
		if (ls == "!" or rs == "!") continue;
		if(s[lf] == '?') res = move(smax(res, smax(ls, rs)));
		if (s[lf] == 'R') res = move(smax(res, rs));
		if (s[lf] == 'L') res = move(smax(res, ls));
	}
	return res;
}
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	cin >> s;
	dp.assign(s.size() + 1, vector<string>(s.size() + 1));
	string res = move(dfs(0, s.size()));
	if (res == "!") res = "invalid";
	cout << res << endl;
	return 0;
}
accuracy: 1
time_ms: 600
memory_kb: 3280
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const string NG = "invalid";

// ??°????????¢?????¨????????\??????
bool num(string s)
{
    bool ret = true;
    rep(i,s.size())if(s[i]<'0' || '9'<s[i]) ret = false;
    if(s[0]=='0') ret = (s=="0");
    return ret;
}

string MAX(string a, string b)
{
    if(a==NG) return b;
    int A = a.size(), B = b.size();
    if(A>B) return a;
    if(A<B) return b;
    return (a<b?b:a);
}

map<string,string> dp;
string E(string s)
{
    int S = s.size();
    if(S==0) return NG;
    if(dp.count(s)) return dp[s];

    string ret = NG;
    if(s[0]=='L' || s[0]=='R')
    {
        if(S<6) return NG;
        if(!(s[1]=='('||s[1]=='?')) return NG;
        if(!(s[S-1]==')'||s[S-1]=='?')) return NG;

        string y = s.substr(2,S-3);
        int Y = y.size();
        for(int i=1; i<Y-1; ++i)
        {
            if(y[i]=='?' || y[i]==',')
            {
                string l = E(y.substr(0,i));
                string r = E(y.substr(i+1));

                if(l!=NG && r!=NG)
                {
                    string x;
                    if(s[0]=='L') x=l;
                    if(s[0]=='R') x=r;
                    ret = MAX(ret,x);
                }
            }
        }
    }
    else if('0'<=s[0] && s[0]<='9')
    {
        rep(i,S)if(s[i]=='?') s[i]='9';
        return (num(s)?s:NG);
    }
    else if(s[0]=='?')
    {
        string candidate = "LR9";
        rep(i,3)
        {
            s[0] = candidate[i];
            string x = E(s);
            if(x!=NG) ret = MAX(ret,x);
        }
    }
    return dp[s]=ret;
}

int main()
{
    string s;
    cin >>s;
    cout << E(s) << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3424
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const string NG = "invalid";

// ??°????????¢?????¨????????\??????
bool num(string s)
{
    bool ret = true;
    rep(i,s.size())if(s[i]<'0' || '9'<s[i]) ret = false;
    if(s[0]=='0') ret = (s=="0");
    return ret;
}

string MAX(string a, string b)
{
    int A = a.size(), B = b.size();
    if(A>B) return a;
    if(A<B) return b;
    rep(i,A)
    {
        if(a[i]>b[i]) return a;
        if(a[i]<b[i]) return b;
    }
    return a;
}

map<string,string> dp;
string E(string s)
{
    int S = s.size();
    if(S==0) return NG;

    if(dp.count(s)) return dp[s];

    string ret = NG;
    if(s[0]=='L' || s[0]=='R' || ('0'<=s[0] && s[0]<='9') || s[0]=='?')
    {
        if(s[0]=='L' || s[0]=='R')
        {
            if(S<6) return NG;
            if(!(s[1]=='('||s[1]=='?')) return NG;
            if(!(s[S-1]==')'||s[S-1]=='?')) return NG;

            string y = s.substr(2,S-3);
            int Y = y.size();
            for(int i=1; i<Y-1; ++i)
            {
                if(y[i]=='?' || y[i]==',')
                {
                    string l = E(y.substr(0,i));
                    string r = E(y.substr(i+1));

                    if(l!=NG && r!=NG)
                    {
                        string x;
                        if(s[0]=='L') x=l;
                        if(s[0]=='R') x=r;

                        if(ret==NG) ret = x;
                        else ret = MAX(ret,x);
                    }
                }
            }
        }
        else if('0'<=s[0] && s[0]<='9')
        {
            rep(i,S)if(s[i]=='?') s[i]='9';
            if(num(s)) return s;
            else return NG;
        }
        else if(s[0]=='?')
        {
            string candidate = "LR9";
            rep(i,3)
            {
                s[0] = candidate[i];
                string x = E(s);
                if(x!=NG)
                {
                    if(ret==NG) ret = x;
                    else ret = MAX(ret,x);
                }
            }
        }
    }
    return dp[s]=ret;
}

int main()
{
    string s;
    cin >>s;
    cout << E(s) << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3448
final_rank: 7

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

int n;
string s;

string maxs(string a,string b){
  if(a=="invalid")return b;
  if(b=="invalid")return a;
  if(a.length()==b.length()){
    return a>b?a:b;
  }else return a.length()>b.length()?a:b;
}

string dfs(int l,int r){
  if(l>=r)return "invalid";
  bool isnum=true;
  string res="invalid";
  repl(i,l,r){
    if(s[i]=='('||s[i]==')'||s[i]=='L'||s[i]=='R'||s[i]==',')isnum=false;
  }
  if(isnum){
    if(s[l]=='0'&&r-l>1)return "invalid";
    res="";
    repl(i,l,r){
      if(s[i]=='?')res+="9";
      else res+=s[i];
    }
  }else{
    if(r-l<4)return "invalid";
    if(s[l]!='L'&&s[l]!='R'&&s[l]!='?')return "invalid";
    if(s[l+1]!='('&&s[l+1]!='?')return "invalid";
    if(s[r-1]!=')'&&s[r-1]!='?')return "invalid";
    repl(i,l+3,r-2){
      if(s[i]!=','&&s[i]!='?')continue;
      string ln=dfs(l+2,i);
      string rn=dfs(i+1,r-1);
      if(ln!="invalid"&&rn!="invalid"){
        if(s[l]=='L')res=maxs(res,ln);
        else if(s[l]=='R')res=maxs(res,rn);
        else res=maxs(res,maxs(ln,rn));
      }
    }
  }
  return res;
}

int main(){
	cin.sync_with_stdio(false);
  cin>>s;
  n=s.length();
  cout<<dfs(0,n)<<endl;
	return 0;
}
accuracy: 1
time_ms: 1060
memory_kb: 3208
final_rank: 15

code_snippet: #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<climits>
#include<cassert>
#include<unordered_map>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)

using namespace std;

string max(string a,string b) {
  if( a.size() == b.size() ) {
    rep(i,(int)a.size()) {
      if( a[i] == b[i] ) continue;
      if( a[i] >  b[i] ) return a;
      else               return b;
    }
    return a;
  } else if( a.size() > b.size() ) {
    return a;
  } else {
    return b;
  }
}

unordered_map<string,string> memo;
string dfs(string s) {
  if( memo.count(s) ) {
    return memo[s];
  }
  bool fin = true;
  rep(i,(int)s.size()) {
    if( s[i] == 'L' || s[i] == 'R' || s[i] == '(' || s[i] == ')' || s[i] == ',' ) {
      fin = false;
      break;
    }
  }
  if( fin ) {
    rep(i,(int)s.size()) {
      if( s[i] == '?' ) s[i] = '9';
    }
    if( s != "0" && s[0] == '0' ) return "";
    return s;
  }
  if( (int)s.size() < 6 ) return "";
  if( !(s[1] == '('||s[1] =='?') || !(s[(int)s.size()-1]==')'||s[(int)s.size()-1]=='?') ) return "";
  string maxi = "";
  if( s[0] == 'L' || s[0] == 'R' || s[0] == '?' ) {
    REP(i,2,(int)s.size()-1) {
      if( s[i] == '?' || s[i] == ',' ) {
	string L = "", R = "";
	REP(j,2,i) L += s[j];
	REP(j,i+1,(int)s.size()-1) R+= s[j];
	string new_L = dfs(L);
	if( new_L.empty() ) continue;
	string new_R = dfs(R);
	if( new_R.empty() ) continue;
	if( s[0] == 'L' || s[0] == 'R' ) {
	  maxi = max(maxi,((s[0]=='L')?new_L:new_R));
	} else {
	  maxi = max(maxi,max(new_L,new_R));
	}
      }
    }
  }
  return memo[s] = maxi;
}

void compute(string s) {
  string answer = dfs(s);
  if( answer.empty() ) puts("invalid");
  else cout << answer << endl;
}

int main() {
  string s;
  cin >> s;
  compute(s);
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3208
final_rank: 4

code_snippet: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<map>
#include<set>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<string>
using namespace std;

typedef long long LL;

string dp[60][60],s;

void update(string &o,string p){
	if(p.empty())	return;
	if(o.size()>p.size())	return;
	if(p.size()>o.size()){
		o=p;	return;
		}
	if(o<p)
		o=p;
	}

string dfs(int l,int r){
	if(l>r)	return "";
	if(!dp[l][r].empty())	return dp[l][r];
	string &res=dp[l][r];
	bool flag=false;
	for(int i=l;i<=r;i++){
		flag|=(s[i]=='L'||s[i]=='R'||s[i]==','||s[i]=='('||s[i]==')');
		}
	if(!flag&&(s[l]!='0'||l==r)){
		for(int i=l;i<=r;i++){
			if(s[i]=='?'){
				res+='9';
				}
			else{
				res+=s[i];
				}
			}
		return res;
		}
	if(s[l+1]!='('&&s[l+1]!='?')	return res;
	if(s[r]!=')'&&s[r]!='?')	return res;
	for(int i=l+3;i<r-1;i++){
		if(s[i]=='?'||s[i]==','){
			dfs(l+2,i-1);
			dfs(i+1,r-1);
			if(dp[l+2][i-1].empty()||dp[i+1][r-1].empty())
				continue;
			if(s[l]=='L'||s[l]=='?')
				update(res,dp[l+2][i-1]);
			if(s[l]=='R'||s[l]=='?')
				update(res,dp[i+1][r-1]);
			}
		}
	return res;
	}

int main(){
	cin>>s;
	auto len=s.size();
	dfs(0,(int)len-1);
	if(dp[0][len-1].empty()){
		printf("invalid\n");
		}
	else{
		cout<< dp[0][len-1] <<endl;
		}
	return 0;
	}
accuracy: 1
time_ms: 10
memory_kb: 3284
final_rank: 5

code_snippet: #include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

bool eq(const string &s, size_t pos, char c2) {
  return pos < s.length() and (s[pos] == c2 or s[pos] == '?');
}

bool is_greater(const string &s1, const string &s2) {
  if (s1 == "invalid") {
    return true;
  } else if (s2 == "invalid") {
    return false;
  }
  return s1.length() == s2.length()? s1 < s2: s1.length() < s2.length();
}

string compute(string s) {
  if ((isdigit(s[0]) and (s.length() == 1 or s[0] != '0')) or s[0] == '?') {
    for (size_t i = 0; i < s.length(); ++i) {
      if (not (isdigit(s[i]) or s[i] == '?')) {
        break;
      }
      if (i == s.length() - 1) {
        for (size_t j = 0; j < s.length(); ++j) {
          if (s[j] == '?') {
            s[j] = '9';
          }
        }
        return s;
      }
    }
  }
  if ((eq(s, 0, 'R') or eq(s, 0, 'L')) and eq(s, 1, '(') and eq(s, s.length() - 1, ')')) {
    vector<string> sv, cands;
    for (size_t i = 3; i < s.length() - 2; ++i) {
      if (eq(s, i, ',')) {
        string sl = compute(s.substr(2, i - 2));
        string sr = compute(s.substr(i + 1, s.length() - i - 2));
        if (sl != "invalid" and sr != "invalid") {
          if (eq(s, 0, 'L')) {
            cands.push_back(sl);
          }
          if (eq(s, 0, 'R')) {
          cands.push_back(sr);
          }
        }
      }
    }
    if (not cands.empty()) {
      return *max_element(cands.begin(), cands.end(), is_greater);
    }
  }
  return "invalid";
}

int main() {
  string s;
  cin >> s;
  cout << compute(s) << endl;
  return 0;
}
accuracy: 1
time_ms: 810
memory_kb: 3192
final_rank: 11

code_snippet: #include "bits/stdc++.h"
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const ll INF = MOD+1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

//typedef int Weight;
//struct Edge
//{
//	int from, to; Weight cost;
//	bool operator < (const Edge& e) const { return cost < e.cost; }
//	bool operator > (const Edge& e) const { return cost > e.cost; }
//};
//typedef vector<Edge> Edges;
//typedef vector<Edges> Graph;
//typedef vector<Weight> Array;
//typedef vector<Array> Matrix;
//void add_edge(Graph &g, int from, int to, Weight cost)
//{
//	g[from].push_back(Edge{ from, to, cost });
//}
//
//void dijkstra(Graph &g, int s, Array &d)
//{
//	d.assign(g.size(), INF);
//	d[s] = 0;
//	typedef pair<Weight, int> P;
//	priority_queue<P, vector<P>, greater<P>> que;
//	que.push(P(0, s));
//	while (!que.empty())
//	{
//		Weight dist = que.top().first;
//		int v = que.top().second;
//		que.pop();
//		if (d[v] < dist) continue;
//		REP(i, g[v].size())
//		{
//			Edge e = g[v][i];
//			if (d[e.to] > d[v] + e.cost)
//			{
//				d[e.to] = d[v] + e.cost;
//				que.push(P(d[e.to], e.to));
//			}
//		}
//	}
//}

string s_max(string a, string b)
{
	if (a.size() > b.size()) return a;
	if (a.size() < b.size()) return b;
	else return max(a, b);
}

string s;

pair<string, bool> solve(int l, int r)
{
	if (l == r) return make_pair("", false);
	//cout << l << " " << r << endl;
	bool f = true;
	FOR(i, l, r)
	{
		if (s[i] != '?' && !isdigit(s[i])) f = false;
	}
	if (f)
	{
		if (r - l > 1 && s[l] == '0') return make_pair("", false);
		string t;
		FOR(i, l, r)
		{
			if (isdigit(s[i])) t += s[i];
			else t += '9';
		}
		return make_pair(t, true);
	}
	if (r - l < 6) return make_pair("", false);
	if (s[l] != 'R' && s[l] != 'L' && s[l] != '?') return make_pair("", false);
	if (s[l + 1] != '(' && s[l + 1] != '?') return make_pair("", false);
	if (s[r - 1] != ')' && s[r - 1] != '?') return make_pair("", false);
	string ans;
	FOR(i, l + 3, r - 2)
	{
		if (s[i] != ',' && s[i] != '?') continue;
		auto lhs = solve(l + 2, i);
		if (lhs.first.size() > 1 && lhs.first[0] == '0') lhs.second = false;
		auto rhs = solve(i + 1, r - 1);
		if (rhs.first.size() > 1 && rhs.first[0] == '0') rhs.second = false;
		if (!(lhs.second && rhs.second)) continue;;
		if (s[l] == 'L') ans = s_max(ans, lhs.first);
		else if (s[l] == 'R') ans = s_max(ans, rhs.first);
		else ans = s_max(ans, s_max(lhs.first, rhs.first));
		//cout << i << " " << ans << endl;
	}
	if (ans == "") return make_pair("", false);
	else
	{
		//cout << l << " " << r <<" "<< ans << endl;
		return make_pair(ans, true);
	}
}

int main()
{
	cin >> s;
	//cout << solve(6, 17).first << endl;
	
	auto ans = solve(0, s.size());
	if (ans.second) cout << ans.first << endl;
	else puts("invalid");
	return 0;
}
accuracy: 1
time_ms: 680
memory_kb: 3200
final_rank: 9

code_snippet: #include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
string dp[100][100];
char in[100];
bool gt(string a,string b){
	if(a[0]=='i')return false;
	if(b[0]=='i')return true;
	if(a.size()!=b.size())return a.size()>b.size();
	for(int i=0;i<a.size();i++)if(a[i]!=b[i])return a[i]>b[i];
	return false;
}
string calc(int a,int b){
	if(dp[a][b].size())return dp[a][b];
	string ret="invalid";
	bool on=true;
	string cur="";
	for(int i=a;i<b;i++){
		if(in[i]=='?')cur+='9';
		else if('0'<=in[i]&&in[i]<='9')cur+=in[i];
		else on=false;
	}
	if(cur.size()>1&&cur[0]=='0')on=false;
	if(on)ret=cur;
	bool L=true;
	bool R=true;
	if(b-a<6||(in[a+1]!='?'&&in[a+1]!='(')||(in[b-1]!='?'&&in[b-1]!=')'))L=R=false;
	if(in[a]!='L'&&in[a]!='?')L=false;
	if(in[a]!='R'&&in[a]!='?')R=false;
	for(int i=a+3;i<b-2;i++){
		if(in[i]!='?'&&in[i]!=',')continue;
		if(L){
			string left=calc(a+2,i);
			string right=calc(i+1,b-1);
			if(left[0]!='i'&&right[0]!='i'){
				if(gt(left,ret))ret=left;
			}
		}
		if(R){
			string left=calc(a+2,i);
			string right=calc(i+1,b-1);
			if(left[0]!='i'&&right[0]!='i'){
				if(gt(right,ret))ret=right;
			}
		}
	}
	//printf("%d %d: %s\n",a,b,ret.c_str());
	return dp[a][b]=ret;
}
int main(){
	scanf("%s",in);
	for(int i=0;i<100;i++)for(int j=0;j<100;j++)dp[i][j]="";
	int n=strlen(in);
	printf("%s\n",calc(0,n).c_str());
}
accuracy: 1
time_ms: 10
memory_kb: 1160
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

string s;
bool f;
string func = "(),RL";
typedef pair<string, bool> psb;

string max_(string a, string b)
{

	if (a.size() > b.size()) return a;
	else if (a.size() < b.size()) return b;
	else return max(a, b);
}

pair<string, bool> solve(int l, int r)
{
	string tmp = s.substr(l, r - l);
	bool ff = true;
	REP(i, func.size())
	{
		if (tmp.find(func[i]) != string::npos) ff = false;
	}
	if (ff)
	{
		string res;
		if (tmp[0] == '0'&& tmp.size() > 1) return psb(res, false);
		FOR(i, l, r)
		{
			if (s[i] == '?') res.push_back('9');
			else res.push_back(s[i]);
		}
		return psb(res, true);
	}
	if  (r-l >= 6 &&
		(s[l] != 'R' || s[l] != 'L' || s[l] != '?') &&
		(s[l+1] == '(' || s[l+1] == '?') &&
		(s[r-1] == ')' || s[r-1] == '?'))
	{
		string res;
		FOR(i, l + 3, r - 2)
		{
			if (s[i] != ','&&s[i] != '?') continue;
			psb lhs = solve(l + 2, i);
			psb rhs = solve(i + 1, r - 1);
			if (lhs.second&&rhs.second)
			{
				string L = lhs.first, R = rhs.first;
				if (s[l] == 'R') res = max_(res, R);
				if (s[l] == 'L') res = max_(res, L);
				if (s[l] == '?')
				{
					res = max_(res, max_(R, L));
				}
			}
		}
		if (res == "") return psb(res, false);
		else return psb(res, true);
	}
	else return psb("", false);
}

int main()
{
	cin >> s;
	f = true;
	psb ans = solve(0, s.size());
	if (!ans.second) puts("invalid");
	else cout << ans.first << endl;
	return 0;
}
accuracy: 1
time_ms: 1990
memory_kb: 3140
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

string s;
bool f;
string func = "(),RL";
typedef pair<string, bool> psb;

pair<string, bool> solve(int l, int r)
{
	string tmp = s.substr(l, r - l);
	bool ff = true;
	REP(i, func.size())
	{
		if (tmp.find(func[i]) != string::npos) ff = false;
	}
	if (ff)
	{
		string res;
		if (tmp[0] == '0'&& tmp.size() > 1) return psb(res, false);
		FOR(i, l, r)
		{
			if (s[i] == '?') res.push_back('9');
			else res.push_back(s[i]);
		}
		return psb(res, true);
	}
	if  (r-l >= 6 &&
		(s[l] != 'R' || s[l] != 'L' || s[l] != '?') &&
		(s[l+1] == '(' || s[l+1] == '?') &&
		(s[r-1] == ')' || s[r-1] == '?'))
	{
		string res = "";
		FOR(i, l+3, r - 2)
		{
			if (s[i] != ','&&s[i] != '?') continue;
			psb lhs = solve(l + 2, i);
			psb rhs = solve(i+1, r - 1);
			if (lhs.second&&rhs.second)
			{
				string L = lhs.first, R = rhs.first;
				if (s[l] == 'R') chmax(res, R);
				if (s[l] == 'L') chmax(res, L);
				if (s[l] == '?')
				{
					if(R.size()>L.size()) chmax(res, R);
					else if (R.size()<L.size()) chmax(res, L);
					else chmax(res, max(lhs.first, rhs.first));
				}
			}
		}
		if (res == "") return psb(res, false);
		else return psb(res, true);
	}
	else return psb("", false);
}

int main()
{
	cin >> s;
	f = true;
	psb ans = solve(0, s.size());
	if (!ans.second) puts("invalid");
	else cout << ans.first << endl;
	return 0;
}
accuracy: 0.8354430379746836
time_ms: 1960
memory_kb: 3136
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef pair<int,int> P;

string str;

bool cmp(string s, string t){
	if(s.size() > t.size()) return true;
	if(s.size() < t.size()) return false;
	rep(i,s.size()){
		if(s[i] > t[i]) return true;
		if(s[i] < t[i]) return false;
	}
	return true;
}

string func(string s){
	if(s == "0") return s;
	bool ok = true;
	string ret;
	rep(i,s.size()){
		if(s[i]=='R'||s[i]=='L'||s[i]=='('||s[i]==')'||s[i]==','){
			ok = false;
			break;
		}
		if(i == 0 && s[i] == '0'){
			return ret;
		}
	}
	if(ok){
		rep(i,s.size()){
			if(s[i] == '?') s[i] = '9';
		}
		return s;
	}
	if(s.size() < 6) return ret;
	
	if(s[0] != 'R' && s[0] != 'L' && s[0] != '?') return ret;
	if((s[1] != '(' && s[1] != '?') || (s[s.size()-1] != ')' && s[s.size()-1] != '?')) return ret;
	for(int i = 3; i <= s.size()-3; i++){
		if(s[i] != ',' && s[i] != '?') continue;
		string l = s.substr(2,i-2);
		string r = s.substr(i+1,s.size()-2-i);
		r = func(r);
		l = func(l);
		if(r.size() == 0 || l.size() == 0) continue;
		if(s[0] == 'R'){
			if(cmp(r,ret)) ret = r;
		}
		if(s[0] == 'L'){
			if(cmp(l,ret)) ret = l;
		}
		if(s[0] == '?'){
			if(cmp(l,r)) r = l;
			if(cmp(r,ret)) ret = r;
		}
	}
	return ret;
}

int main(){
	cin >> str;
	string out = func(str);
	if(out.size() == 0) puts("invalid");
	else cout << out << endl;
}
accuracy: 1
time_ms: 1630
memory_kb: 1196
final_rank: 3

code_snippet: #include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
#include <iostream>
using namespace std;

string str;
string dp[51][51];

string memo(int l,int r){
	if(dp[l][r]!="")return dp[l][r];
	bool flag=true;
	string num="";
	for(int i=l;i<r;i++){
		if(str[i]>='0' && str[i]<='9')num+=str[i];
		else if(str[i]=='?')num+='9';
		else flag=false;
	}
	if(flag && (num[0]!='0' || num.size()==1) && num.size()>0){
		return (dp[l][r]=num);
	}
	flag=false;
	string res="";
	if(str[l+1]=='?' || str[l+1]=='('){
		if(str[r-1]=='?' || str[r-1]==')'){
			for(int i=l+2;i<r-1;i++){
				if(str[i]==',' || str[i]=='?'){
					string left=memo(l+2,i);
					string right=memo(i+1,r-1);
					if(left=="invalid" || right=="invalid")continue;
					if(str[l]=='?' || str[l]=='L'){
						if(res.size()<left.size() || (res.size()==left.size() && res<left)){
							res=left;
							flag=true;
						}
					}
					if(str[l]=='?' || str[l]=='R'){
						if(res.size()<right.size() || (res.size()==right.size() && res<right)){
							res=right;
							flag=true;
						}
					}
				}
			}
		}
	}
	return flag?(dp[l][r]=res):"invalid";
}

int main(void){
	cin >> str;
	for(int i=0;i<=str.size();i++){
		for(int j=0;j<=str.size();j++){
			dp[i][j]="";
		}
	}
	cout << memo(0,str.size()) << endl;
	return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 1264
final_rank: 2