Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

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

Solution in Ruby (judge verdict: Accepted)

def f(i, j)
    return nil if S[i] =~ /[(),]/

    if S[i] == ?0
        if i == j
            return 0
        else
            return nil
        end
    end

    if S[i] =~ /\d/
        if S[i..j] =~ /[LR,()]/
            return nil
        else
            return S[i..j].tr(??, ?9).to_i
        end
    end

    if S[i] == ?L
        return nil if j - i < 5
        return nil if S[i+1] !~ /[(?]/
        return nil if S[j] !~ /[)?]/
        return l(i+2, j-1)
    end

    if S[i] == ?R
        return nil if j - i < 5
        return nil if S[i+1] !~ /[(?]/
        return nil if S[j] !~ /[)?]/
        return r(i+2, j-1)
    end

    if S[i..j] !~ /[LR,()]/
        return S[i..j].tr(??, ?9).to_i
    end

    return nil if j - i < 5
    return nil if S[i+1] !~ /[(?]/
    return nil if S[j] !~ /[)?]/

    x(i+2, j-1)
end

def l(i, j)
    return nil if j - i <= 1
    max = - Float::INFINITY
    (j-1).downto(i+1) do |k|
        break if 10 ** (k - i) <= max
        next if S[k] !~ /[?,]/
        next if ! f(k+1, j)
        if t = f(i, k-1)
            max = t if t > max
        end
    end
    max >= 0 ? max : nil
end

def r(i, j)
    return nil if j - i <= 1
    max = - Float::INFINITY
    (i+1..j-1).each do |k|
        break if 10 ** (j - k) <= max
        next if S[k] !~ /[?,]/
        next if ! f(i, k-1)
        if t = f(k+1, j)
            max = t if t > max
        end
    end
    max >= 0 ? max : nil
end

def x(i, j)
    return nil if j - i <= 1
    max = - Float::INFINITY
    (i+1..j-1).each do |k|
        next if S[k] !~ /[?,]/
        if (a = f(i, k-1)) && (b = f(k+1, j))
            max = [max, a, b].max
        end
    end
    max >= 0 ? max : nil
end

module Memoizable
	def memoize(name)
		original = "__original_#{name}__"
		([Class, Module].include?(self.class) ? self : self.class).class_eval {
			alias_method(original, name)
			private(original)
			cache = {}
			define_method(name){|*args|
				cache[args] ||= send(original, *args)
			}
		}
	end
end
include Memoizable

memoize :f
memoize :l
memoize :r
memoize :x

S = gets.chomp
puts f(0, S.size-1) || 'invalid'