Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #!/bin/python
  2.  
  3. import sys
  4. memo = {}
  5. def maxStocks(k, arrHash):
  6. key = str(k)+str(arrHash)
  7. if key in memo:
  8. return memo[key]
  9. if k <= 0:
  10. return 0
  11. Max = 0
  12. for key in arrHash:
  13. if arrHash[key] > 0:
  14. arrHash[key] -= 1
  15. new_k = k - key
  16. if new_k > 0:
  17. Max = max(Max, maxStocks(new_k, arrHash) + 1 )
  18. arrHash[key] += 1
  19. memo[key] = Max
  20. return Max
  21.  
  22.  
  23. def buyMaximumProducts(n, k, a):
  24. arrHash = {}
  25. for i in range(n):
  26. priceperunit = a[i]/(i + 1.0)
  27. arrHash[a[i]] = priceperunit
  28. arranged = sorted([ ( a[i], i + 1 ) for i in range(n) ], key = lambda x:x[0]/float(x[1]) )
  29. cumulative = 0
  30. total_money = k
  31. count = 0
  32. for value, amount in arranged:
  33. if value * amount > total_money:
  34. amount = total_money/value
  35. if amount == 0:
  36. break
  37. count += amount
  38. total_money -= value * amount
  39.  
  40. return count
  41.  
  42.  
  43.  
  44. # Complete this function
  45.  
  46. if __name__ == "__main__":
  47. n = int(raw_input().strip())
  48. arr = map(int, raw_input().strip().split(' '))
  49. k = long(raw_input().strip())
  50. result = buyMaximumProducts(n, k, arr)
  51. print result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement