human
Maximum Sum Subsequence of length k | Python program to calculate the maximum sum of increasing subsequence of length k.
In the implementation dp [ n ] [ k ] represents maximum sum subsequence of length k and the subsequence is ending at index n ..
Initializing whole multidimensional dp array with value - 1.
For each ith position increasing subsequence of length 1 is equal to that array ith value so initializing dp [ i ] [ 1 ] with that array value.
Starting from 1 st index as we have calculated for 0 th index . Computing optimized dp values in bottom - up manner.
check for increasing subsequence.
Proceed if value is pre calculated.
Check for all the subsequences ending at any j < i and try including element at index i in them for some length l . Update the maximum value for every length ..
The final result would be the maximum value of dp [ i ] [ k ] for all different i ..
When no subsequence of length k is possible sum would be considered zero.
Driver Code"
How can the above be solved in Python?

Give me a code to maximum Sum Subsequence of length k and the other steps required?
assistant
#Here is code to maximum Sum Subsequence of length k and the other steps required.
 def MaxIncreasingSub ( arr , n , k ) :
	dp = [ - 1 ] * n
	ans = - 1
	for i in range ( n ) :
		dp [ i ] = [ - 1 ] * ( k + 1 )
	for i in range ( n ) :
		dp [ i ] [ 1 ] = arr [ i ]
	for i in range ( 1 , n ) :
		for j in range ( i ) :
			if arr [ j ] < arr [ i ] :
				for l in range ( 1 , k ) :
					if dp [ j ] [ l ] != - 1 :
						dp [ i ] [ l + 1 ] = max ( dp [ i ] [ l + 1 ] , dp [ j ] [ l ] + arr [ i ] )
	for i in range ( n ) :
		if ans < dp [ i ] [ k ] :
			ans = dp [ i ] [ k ]
	return ( 0 if ans == - 1 else ans )
if __name__ == " _ _ main _ _ " :
	n , k = 8 , 3
	arr = [ 8 , 5 , 9 , 10 , 5 , 6 , 21 , 8 ]
	ans = MaxIncreasingSub ( arr , n , k )
	print ( ans )