Minimum cost to select K strictly increasing elements | Python3 program for the above approach ; Function to calculate min cost to choose k increasing elements ; If k elements are counted return 0 ; If all elements of array has been traversed then return MAX_VALUE ; To check if this is already calculated ; When i 'th elements  is not considered ; When the ith element is greater than previous element check if adding its cost makes total cost minimum ; Driver code
N = 1005 ; NEW_LINE K = 20 ; NEW_LINE n = 0 NEW_LINE k = 0 NEW_LINE dp = [ [ [ - 1 for k in range ( K + 1 ) ] for j in range ( N + 1 ) ] for i in range ( N + 1 ) ] NEW_LINE def minCost ( i , prev , cnt , ele , cost ) : NEW_LINE INDENT if ( cnt == k + 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( i == n + 1 ) : NEW_LINE INDENT return 100000 ; NEW_LINE DEDENT ans = dp [ i ] [ prev ] [ cnt ] ; NEW_LINE if ( ans != - 1 ) : NEW_LINE INDENT return ans ; NEW_LINE DEDENT ans = minCost ( i + 1 , prev , cnt , ele , cost ) ; NEW_LINE if ( ele [ i ] > ele [ prev ] ) : NEW_LINE INDENT ans = min ( ans , cost [ i ] + minCost ( i + 1 , i , cnt + 1 , ele , cost ) ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE k = 2 ; NEW_LINE ele = [ 0 , 2 , 6 , 4 , 8 ] NEW_LINE cost = [ 0 , 40 , 20 , 30 , 10 ] NEW_LINE ans = minCost ( 1 , 0 , 1 , ele , cost ) ; NEW_LINE if ( ans == 100000 ) : NEW_LINE INDENT ans = - 1 ; NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT