human
Maximum sum after rearranging the array for K queries | Function to find maximum sum after rearranging array elements.
Auxiliary array to find the count of each selected elements Initialize with 0.
Finding count of every element to be selected.
Making it to 0 - indexing.
Prefix sum array concept is used to obtain the count array.
Iterating over the count array to get the final array.
Variable to store the maximum sum.
Sorting both the arrays.
Loop to find the maximum sum.
Driver code"
Solution in Python:

Can you provide a code to maximum sum after rearranging the array for K queries and the other steps required?
assistant
#Here is code to maximum sum after rearranging the array for K queries and the other steps required.
 def maxSumArrangement ( A , R , N , M ) :
	count = [ 0 for i in range ( N ) ]
	for i in range ( M ) :
		l = R [ i ] [ 0 ]
		r = R [ i ] [ 1 ] + 1
		l = l - 1
		r = r - 1
		count [ l ] = count [ l ] + 1
		if ( r < N ) :
			count [ r ] = count [ r ] - 1
	for i in range ( 1 , N ) :
		count [ i ] = count [ i ] + count [ i - 1 ]
	ans = 0
	count . sort ( )
	A . sort ( )
	for i in range ( N - 1 , - 1 , - 1 ) :
		ans = ans + A [ i ] * count [ i ]
	return ans
A = [ 2 , 6 , 10 , 1 , 5 , 6 ]
R = [ [ 1 , 3 ] , [ 4 , 6 ] , [ 3 , 4 ] ]
N = len ( A )
M = len ( R )
print ( maxSumArrangement ( A , R , N , M ) )