Minimum number that can be obtained by applying ' + ' and ' * ' operations on array elements | Function to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the string S ; Stores the count of multiplication operator in the string ; Store the required result ; Iterate in the range to create the mask ; Checking the number of bits that are set in the mask ; Check if the number of bits that are set in the mask is multiplication operation ; Storing the elements that is to be added ; Apply the multiplications operation first ; If sign is ' * ' , then multiply last element of deque with arr [ i ] ; append last multiplied element in the deque ; If the element is to be added , then add it to the deque ; Add all the element of the deque ; Minimize the answer with the given sum ; Driver Code
def minimumSum ( A , N , S ) : NEW_LINE INDENT mul = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( S [ i ] == " * " ) : NEW_LINE INDENT mul += 1 NEW_LINE DEDENT DEDENT ans = 1000000 NEW_LINE for i in range ( 1 << ( N - 1 ) ) : NEW_LINE INDENT cnt = 0 NEW_LINE v = [ ] NEW_LINE for j in range ( N - 1 ) : NEW_LINE INDENT if ( ( 1 << j ) & i ) : NEW_LINE INDENT cnt += 1 NEW_LINE v . append ( " * " ) NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( " + " ) NEW_LINE DEDENT DEDENT if ( cnt == mul ) : NEW_LINE INDENT d = [ ] NEW_LINE d . append ( A [ 0 ] ) NEW_LINE for j in range ( N - 1 ) : NEW_LINE INDENT if ( v [ j ] == " * " ) : NEW_LINE INDENT x = d [ len ( d ) - 1 ] NEW_LINE d . pop ( ) NEW_LINE x = x * A [ j + 1 ] NEW_LINE d . append ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT d . append ( A [ j + 1 ] ) NEW_LINE DEDENT DEDENT sum = 0 NEW_LINE while ( len ( d ) > 0 ) : NEW_LINE INDENT x = d [ 0 ] NEW_LINE sum += x NEW_LINE d . pop ( 0 ) NEW_LINE DEDENT ans = min ( ans , sum ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = [ 2 , 2 , 2 , 2 ] NEW_LINE S = " * * + " NEW_LINE N = len ( A ) NEW_LINE print ( minimumSum ( A , N , S ) ) NEW_LINE