human
Maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence | Function to find the maximum sum subsequence.
Base Case.
If current state is already calculated then use it.
If current element is positive.
Update ans and recursively call with update value of flag.
Else current element is negative.
Update ans and recursively call with update value of flag.
Return maximum sum subsequence.
Function that finds the maximum sum of element of the subsequence with alternate + ve and - ve signs.
Create auxiliary array dp [ ] [ ].
Function call.
Driver Code.
Given array arr [ ].
Function call"
Solution in Python:
assistant
#Here is code to maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence and the other steps required.
 def findMax ( a , dp , i , flag ) :
	if ( i == len ( a ) ) :
		return 0
	if ( dp [ i ] [ flag ] != - 1 ) :
		return dp [ i ] [ flag ]
	ans = 0
	if ( flag == 0 ) :
		ans = max ( findMax ( a , dp , i + 1 , 0 ) , a [ i ] + findMax ( a , dp , i + 1 , 1 ) )
	else :
		ans = max ( findMax ( a , dp , i + 1 , 1 ) , - 1 * a [ i ] + findMax ( a , dp , i + 1 , 0 ) )
	dp [ i ] [ flag ] = ans
	return ans
def findMaxSumUtil ( arr , N ) :
	dp = [ [ - 1 for i in range ( 2 ) ] for i in range ( N ) ]
	print ( findMax ( arr , dp , 0 , 0 ) )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 3 , 4 , 1 , 2 ]
	N = len ( arr )
	findMaxSumUtil ( arr , N )