human
Maximize the Sum of the given array using given operations | Comparator to sort the array in ascending order.
Function to maximise the sum of the given array.
Stores { A [ i ] , B [ i ] } pairs.
Sort in descending order of the values in the array A [ ].
Stores the maximum sum.
If B [ i ] is equal to 0.
Simply add A [ i ] to the sum.
Add the highest K numbers.
Subtract from the sum.
Return the sum.
Driver Code"
Solution in Python:
assistant
#Here is code to maximize the Sum of the given array using given operations and the other steps required.
 def compare ( p1 , p2 ) :
	return p1 [ 0 ] > p2 [ 0 ]
def maximiseScore ( A , B , K , N ) :
	pairs = [ ]
	for i in range ( N ) :
		pairs . append ( [ A [ i ] , B [ i ] ] )
	pairs . sort ( key = lambda x : x [ 0 ] , reverse = True )
	Sum = 0
	for i in range ( N ) :
		if ( pairs [ i ] [ 1 ] == 0 ) :
			Sum += pairs [ i ] [ 0 ]
		elif ( pairs [ i ] [ 1 ] == 1 ) :
			if ( K > 0 ) :
				Sum += pairs [ i ] [ 0 ]
				K -= 1
			else :
				Sum -= pairs [ i ] [ 0 ]
	return Sum
A = [ 5 , 4 , 6 , 2 , 8 ]
B = [ 1 , 0 , 1 , 1 , 0 ]
K = 2
N = len ( A )
print ( maximiseScore ( A , B , K , N ) )