<p>You are given a <strong>0-indexed</strong> string <code>expression</code> of the form <code>&quot;&lt;num1&gt;+&lt;num2&gt;&quot;</code> where <code>&lt;num1&gt;</code> and <code>&lt;num2&gt;</code> represent positive integers.</p>

<p>Add a pair of parentheses to <code>expression</code> such that after the addition of parentheses, <code>expression</code> is a <strong>valid</strong> mathematical expression and evaluates to the <strong>smallest</strong> possible value. The left parenthesis <strong>must</strong> be added to the left of <code>&#39;+&#39;</code> and the right parenthesis <strong>must</strong> be added to the right of <code>&#39;+&#39;</code>.</p>

<p>Return <code>expression</code><em> after adding a pair of parentheses such that </em><code>expression</code><em> evaluates to the <strong>smallest</strong> possible value.</em> If there are multiple answers that yield the same result, return any of them.</p>

<p>The input has been generated such that the original value of <code>expression</code>, and the value of <code>expression</code> after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> expression = &quot;247+38&quot;
<strong>Output:</strong> &quot;2(47+38)&quot;
<strong>Explanation:</strong> The <code>expression</code> evaluates to 2 * (47 + 38) = 2 * 85 = 170.
Note that &quot;2(4)7+38&quot; is invalid because the right parenthesis must be to the right of the <code>&#39;+&#39;</code>.
It can be shown that 170 is the smallest possible value.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> expression = &quot;12+34&quot;
<strong>Output:</strong> &quot;1(2+3)4&quot;
<strong>Explanation:</strong> The expression evaluates to 1 * (2 + 3) * 4 = 1 * 5 * 4 = 20.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> expression = &quot;999+999&quot;
<strong>Output:</strong> &quot;(999+999)&quot;
<strong>Explanation:</strong> The <code>expression</code> evaluates to 999 + 999 = 1998.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= expression.length &lt;= 10</code></li>
	<li><code>expression</code> consists of digits from <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code> and <code>&#39;+&#39;</code>.</li>
	<li><code>expression</code> starts and ends with digits.</li>
	<li><code>expression</code> contains exactly one <code>&#39;+&#39;</code>.</li>
	<li>The original value of <code>expression</code>, and the value of <code>expression</code> after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.</li>
</ul>

class Solution:
    def minimizeResult(self, expression: str) -> str:
        '''
        3 ways 2 ways
        247.   38
        2,47   3,8
        24,7.  
        '''
        num_list = expression.split('+')
        n1_list = list(num_list[0])
        n2_list = list(num_list[1])

        num1_pairs = []
        adding_num = 0
        for i in range(len(n1_list)):
            num1_pairs.append((adding_num, int(''.join(n1_list[i:]))))
            adding_num = (adding_num)*10+int(n1_list[i])
        
        num2_pairs = []
        adding_num = 0    
        for i in range(len(n2_list)):
            adding_num = (adding_num)*10+int(n2_list[i])
            if i+1<len(n2_list):
                num2_pairs.append((adding_num, int(''.join(n2_list[i+1:]))))
            else: num2_pairs.append((adding_num, 0))

        min_num = float('inf')
        for num1 in num1_pairs:
            for num2 in num2_pairs:
                a, b = num1
                a = a if a else 1
                b = b if b else 1
                
                c, d = num2
                c = c if c else 1
                d = d if d else 1
                num= a*d*(b+c)
                if min_num>num:
                    min_num = num
                    ans = [num1, num2]
        num1, num2 = ans
        a, b, c, d = str(num1[0]),str(num1[1]),str(num2[0]),str(num2[1])
        stack = []
        if a !='0': stack.append(a)
        stack.append('(') 
        if b !='0': stack.append(b)
        stack.append('+') 
        if c !='0': stack.append(c)
        stack.append(')') 
        if d !='0': stack.append(d)
        return ''.join(stack)

        # while n1p <len(num_list[0]) and n2p <len(num_list[1]):
        #     n1 = num_list[0][n1p:]
        #     n2 = num_list[1][n2p:]
28