human
Maximize the profit after selling the tickets | Set 2 ( For elements in range [ 1 , 10 ^ 6 ] ) | Function to find maximum profit after selling K tickets.
Frequency array to store freq of every element of the array.
Modify the arr [ ] so that the array is sorted in O ( N ).
Variable to store answer.
Traverse the array while K > 0 and j >= 0.
If arr [ i ] > arr [ j ] then ticket can be brought from counter [ j + 1 , N ].
If arr [ j ] = = arr [ i ] decrement j until arr [ j ] != arr [ i ].
Sell tickets from counter [ j + 1 , N ].
All elements of array are equal Send tickets from each counter 1 time until K > 0..
Converting answer from long long to int.
Driver Code.
Given Input.
Function Call"
Solution in Python:
 ... Give me a code to maximize the profit after selling the tickets and the other steps required?
assistant
#Here is code to maximize the profit after selling the tickets and the other steps required.
 def maxAmount ( n , k , arr ) :
	A = [ 0 for i in range ( 1000001 ) ]
	for i in range ( n ) :
		A [ arr [ i ] ] += 1
	j = 0
	for j in range ( 1000001 ) :
		while ( A [ i ] != 0 ) :
			arr [ j ] = i ;
			j += 1
			A [ i ] -= 1
	ans = 6
	mod = 1000000007
	i = n - 1
	j = n - 2
	while ( k > 0 and j >= 0 ) :
		if ( arr [ i ] > arr [ j ] ) :
			ans = ans + min ( k , ( i - j ) ) * arr [ i ]
			k = k - ( i - j )
			arr [ i ] -= 1
		else :
			while ( j >= 0 and arr [ j ] == arr [ i ] ) :
				j -= 1
			if ( j < 0 ) :
				break
			ans = ans + min ( k , ( i - j ) ) * arr [ i ]
			k = k - ( i - j )
			arr [ i ] -= 1
	while ( k > 0 and arr [ i ] != 0 ) :
		ans = ans + min ( n , k ) * arr [ i ]
		k -= n
		arr [ i ] -= 1
	ans = ans % mod
	x = ans
	return x
if __name__ == ' _ _ main _ _ ' :
	n = 5
	k = 3
	arr = [ 4 , 3 , 6 , 2 , 4 ]
	ans = maxAmount ( n , k , arr )
	print ( ans )