Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def last(n):
- return n[2]
- if __name__=="__main__":
- print("Enter the no of items:")
- n=int(input())
- print("Enter capacity of knapsack:")
- c=int(input())
- l=[]
- for i in range(n):
- w,v=input().split()
- w=int(w)
- v=int(v)
- tup=w,v,v/w
- l.append(tup)
- l.sort(key=lambda x:x[2])
- prof=0
- for i in range(n-1,-1,-1):
- if l[i][0]<=c:
- prof+=l[i][1]
- print(l[i][1])
- c-=l[i][0]
- else:
- prof+=c*l[i][2]
- print(c,l[i][2])
- print("The total Profit is ",prof)
Add Comment
Please, Sign In to add comment