Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/python
- import sys
- memo = {}
- def maxStocks(k, arrHash):
- key = str(k)+str(arrHash)
- if key in memo:
- return memo[key]
- if k <= 0:
- return 0
- Max = 0
- for key in arrHash:
- if arrHash[key] > 0:
- arrHash[key] -= 1
- new_k = k - key
- if new_k > 0:
- Max = max(Max, maxStocks(new_k, arrHash) + 1 )
- arrHash[key] += 1
- memo[key] = Max
- return Max
- def buyMaximumProducts(n, k, a):
- arrHash = {}
- for i in range(n):
- priceperunit = a[i]/(i + 1.0)
- arrHash[a[i]] = priceperunit
- arranged = sorted([ ( a[i], i + 1 ) for i in range(n) ], key = lambda x:x[0]/float(x[1]) )
- cumulative = 0
- total_money = k
- count = 0
- for value, amount in arranged:
- if value * amount > total_money:
- amount = total_money/value
- if amount == 0:
- break
- count += amount
- total_money -= value * amount
- return count
- # Complete this function
- if __name__ == "__main__":
- n = int(raw_input().strip())
- arr = map(int, raw_input().strip().split(' '))
- k = long(raw_input().strip())
- result = buyMaximumProducts(n, k, arr)
- print result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement