human
Minimum cost to sort an Array such that swapping X and Y costs XY | Function returns the minimum cost to sort the given array.
Create array of pairs in which 1 st element is the array element and 2 nd element is index of first.
Initialize the total cost.
Sort the array with respect to array value.
Initialize the overall minimum which is the 1 st element.
To keep track of visited elements create a visited array & initialize all elements as not visited.
Iterate over every element of the array.
If the element is visited or in the sorted position , and check for next element.
Create a vector which stores all elements of a cycle.
It covers all the elements of a cycle.
If cycle is found then the swapping is required.
Initialize local minimum with 1 st element of the vector as it contains the smallest element in the beginning.
Stores the cost with using only local minimum value ..
Stores the cost of using both local minimum and overall minimum.
Update the result2.
Store the minimum of the two result to total cost.
Return the minimum cost.
Given array arr [ ].
Function call"
How can the above be solved in Python?

What is a code to minimum cost to sort an Array such that swapping X and Y costs XY and the other steps required?
assistant
#Here is code to minimum cost to sort an Array such that swapping X and Y costs XY and the other steps required.
 def minCost ( arr , n ) :
	sortedarr = [ ]
	total_cost = 0
	for i in range ( n ) :
		sortedarr . append ( [ arr [ i ] , i ] )
	sortedarr . sort ( )
	overall_minimum = sortedarr [ 0 ] [ 0 ]
	vis = [ False ] * n
	for i in range ( n ) :
		if vis [ i ] and sortedarr [ i ] [ 1 ] == i :
			continue
		v = [ ]
		j = i
		size = 0
		while vis [ j ] == False :
			vis [ j ] = True
			v . append ( sortedarr [ j ] [ 0 ] )
			j = sortedarr [ j ] [ 1 ]
			size += 1
		if size != 0 :
			local_minimum = v [ 0 ]
			result1 = 0
			result2 = 0
			for k in range ( 1 , size ) :
				result1 += local_minimum * v [ k ]
			for k in range ( size ) :
				result2 += overall_minimum * v [ k ]
			result2 += ( overall_minimum * local_minimum )
			total_cost += min ( result1 , result2 )
	return total_cost
A = [ 1 , 8 , 9 , 7 , 6 ]
ans = minCost ( A , len ( A ) )
print ( ans )