def fractional_knapsack():1weights=[10,20,30]2values=[60,100,120]3capacity=504res=05# Pair : [Weight,value]6for pair in sorted(zip(weights,values), key= lambda x: x[1]/x[0], reverse=True):7if capacity<=0: # Capacity completed - Bag fully filled8break9if pair[0]>capacity: # Current's weight with highest value/weight ratio Available Capacity10res+=int(capacity * (pair[1]/pair[0])) # Completely fill the bag11capacity=012elif pair[0]<=capacity: # Take the whole object13res+=pair[1]14capacity-=pair[0]15print(res)1617if __name__=="__main__":18fractional_knapsack()1920