This is a memo for myself.
▼ Question
--A list containing the prices of each toy is given. --Calculate the maximum number of toys that can be purchased with your money (k).
▼sample input
python
prices = [1,12,5,111,200,1000,10]
k=50
▼sample output
python
4
▼my answer
python
def maximumToys(prices, k):
    ans=total=0
    if min(prices) > k:
        return 0
    for price in sorted(prices):
        total += price
        if total <= k:
            ans+=1
    return ans
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    nk = input().split()
    n = int(nk[0])
    k = int(nk[1])
    prices = list(map(int, input().rstrip().split()))
    result = maximumToys(prices, k)
    fptr.write(str(result) + '\n')
    fptr.close()
Recommended Posts