MACBALL - Editorial

PROBLEM LINK:

Practice
Contest

Author: Riddhish Lichade
Tester: Prathamesh Sogale
Editorialist: Ram Agrawal

DIFFICULTY:

CAKEWALK

PROBLEM:

Given a list of cost of items and the total money Meena has, Meena wants to buy a cricket ball which costs B rupees. Meena wants to buy the ball at any cost. He decides to reduce some items from the list if he falls short of money. You have to find the maximum number of items Meena will buy.

EXPLANATION:

To find the maximum number of items, let’s sort the given array of costs in descending order. Now traverse the array and check whether reducing one item after another makes Meena able to buy the cricket ball.

SOLUTIONS:

Setter's Solution
from sys import stdin
for _ in range(int(stdin.readline())):
    x, n, b = map(int, stdin.readline().strip().split())
    a = list(map(int, stdin.readline().strip().split()))
    tot=sum(a)+b
    if(tot<=x):
        print(n)
        continue
    a.sort(reverse=True)
    i=0
    while(tot>x):
        tot-=a[i]
        i+=1
        n-=1
    print(n)