Theft at World Bank | Python3 program for the above approach ; Custom comparator ; Function to find the maximum profit ; Stores the pairs of elements of B and A at the same index ; Iterate over the range [ 0 , N ] ; If current integer is perfect square ; Push the pair of B [ i ] and A [ i ] in vector V ; Sort the vector using the custom comparator ; Stores the maximum profit ; Traverse the vector V ; If V [ i ] [ 1 ] is less than W ; Increment profit by V [ i ] [ 0 ] ; Decrement V [ i ] [ 0 ] from W ; Otherwise ; Update profit ; Return the value of profit ; Driver Code
import math NEW_LINE from functools import cmp_to_key NEW_LINE def comparator ( p1 , p2 ) : NEW_LINE INDENT a = p1 [ 0 ] NEW_LINE b = p1 [ 1 ] NEW_LINE c = p2 [ 0 ] NEW_LINE d = p2 [ 1 ] NEW_LINE val1 = a / b NEW_LINE val2 = c / d NEW_LINE return val1 > val2 NEW_LINE DEDENT def maximumProfit ( A , B , N , W ) : NEW_LINE INDENT V = [ ] NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT temp = int ( math . sqrt ( A [ i ] ) ) NEW_LINE if temp * temp == A [ i ] : NEW_LINE INDENT continue NEW_LINE DEDENT V . append ( [ B [ i ] , A [ i ] ] ) NEW_LINE DEDENT V = sorted ( V , key = cmp_to_key ( comparator ) ) NEW_LINE profit = 0.00 NEW_LINE k = len ( V ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT if V [ i ] [ 1 ] <= W : NEW_LINE INDENT profit += V [ i ] [ 0 ] NEW_LINE W -= V [ i ] [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT profit += ( V [ i ] [ 0 ] * W ) / V [ i ] [ 1 ] NEW_LINE break NEW_LINE DEDENT DEDENT return profit NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE W = 10 NEW_LINE A = [ 4 , 5 , 7 ] NEW_LINE B = [ 8 , 5 , 4 ] NEW_LINE print ( round ( maximumProfit ( A , B , N , W ) , 5 ) ) NEW_LINE DEDENT