human
Write me a code to minimum cost to buy N kilograms of sweet for M persons and the other steps required given the following:
Minimum cost to buy N kilograms of sweet for M persons | Function to find the minimum cost of sweets.
Defining the sweet array.
DP array to store the values.
Since index starts from 1 we reassign the array into sweet.
Assigning base cases for dp array.
At 0 it is free.
Package not available for desirable amount of sweets.
Buying the ' k ' kg package and assigning it to dp array.
If no solution , select from previous k - 1 packages.
If solution does not exist.
Print the solution.
Driver Function.
Calling the desired function"
Solution in Python:
assistant
#Here is code to minimum cost to buy N kilograms of sweet for M persons and the other steps required.
 def find ( m , n , adj ) :
	sweet = [ 0 ] * ( n + 1 )
	dp = [ [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
	sweet [ 0 ] = 0
	for i in range ( 1 , m + 1 ) :
		sweet [ i ] = adj [ i - 1 ]
	for i in range ( m + 1 ) :
		for k in range ( n + 1 ) :
			dp [ i ] [ 0 ] [ k ] = 0
		for k in range ( 1 , n + 1 ) :
			dp [ i ] [ k ] [ 0 ] = - 1
	for i in range ( m + 1 ) :
		for j in range ( 1 , n + 1 ) :
			for k in range ( 1 , n + 1 ) :
				dp [ i ] [ j ] [ k ] = - 1
				if ( i > 0 and j >= k and sweet [ k ] > 0 and dp [ i - 1 ] [ j - k ] [ k ] != - 1 ) :
					dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j - k ] [ k ] + sweet [ k ]
				if ( dp [ i ] [ j ] [ k ] == - 1 or ( dp [ i ] [ j ] [ k - 1 ] != - 1 and dp [ i ] [ j ] [ k ] > dp [ i ] [ j ] [ k - 1 ] ) ) :
					dp [ i ] [ j ] [ k ] = dp [ i ] [ j ] [ k - 1 ]
	if ( dp [ m ] [ n ] [ n ] == - 1 ) :
		return 0
	else :
		return dp [ m ] [ n ] [ n ]
m = 3
adj = [ 2 , 1 , 3 , 0 , 4 , 10 ]
n = len ( adj )
print ( find ( m , n , adj ) )