Cost of creating smallest subsequence with sum of difference between adjacent elements maximum |  ; Initializing cost = 0 ; To store the removed element ; This will store the sum of the subsequence ; Checking all the element of the vector ; Storing the value of arr [ i ] in temp variable ; If the situation like arr [ i - 1 ] < arr [ i ] < arr [ i + 1 ] or arr [ i - 1 ] > arr [ i ] > arr [ i + 1 ] occur remove arr [ i ] i . e , temp from sequence ; Insert the element in the set removedElements ; Storing the value of arr [ i ] in temp ; Taking the element not in removedElements ; Adding the value of elements of subsequence ; If we have to remove the element then we need to add the cost associated with the element ; Printing the sum of the subsequence with minimum length possible ; Printing the cost incurred in creating subsequence ; Driver code ; Calling the function
def costOfSubsequence ( N , arr , costArray ) : NEW_LINE INDENT i , temp , cost = 0 , 0 , 0 NEW_LINE removedElements = { ' ' } NEW_LINE ans = 0 NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE if ( ( ( arr [ i - 1 ] < temp ) and ( temp < arr [ i + 1 ] ) ) or ( ( arr [ i - 1 ] > temp ) and ( temp > arr [ i + 1 ] ) ) ) : NEW_LINE INDENT removedElements . add ( temp ) NEW_LINE DEDENT DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE if ( temp not in removedElements ) : NEW_LINE INDENT ans = ans + arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT cost += costArray [ i ] NEW_LINE DEDENT DEDENT print ( ans , end = " , ▁ " ) NEW_LINE print ( cost ) NEW_LINE DEDENT N = 4 NEW_LINE arr = [ 1 , 3 , 4 , 2 ] NEW_LINE costArray = [ 0 , 1 , 0 , 0 ] NEW_LINE costOfSubsequence ( N , arr , costArray ) NEW_LINE