Problem: There are N children, numbered 1, 2, ..., N.  Snuke has decided to distribute x sweets among them. He needs to give out all the x sweets, but some of the children may get zero sweets.  For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly a_i sweets. Snuke is trying to maximize the number of happy children by optimally distributing the sweets. Find the maximum possible number of happy children.

Submission attempts (in order):
# AtCorder-Candy Distribution Again

n,x = map(int, input().split())
list[n] = list(map(int, input().split()))

a = 0
i = 0
list.sort()

if x < list[0]:
    print(0)

elif x >= list[0]:

    while x >= list[i] and x > 0:
        x -= list[i]
        a += 1
        if i < n-1 and x < list[i+1]:
            break
        if i == n-1:
            if x > list[i]:
                a = n-1
            else:
                a = n
            break
        i += 1

    print(a)


# AtCorder-Candy Distribution Again

n,x = map(int, input().split())
list = list(map(int, input().split()))

a = 0
i = 0
list.sort()

if x < list[0]:
    print(0)

elif x >= list[0]:

    while x >= list[i] and x > 0:
        x -= list[i]
        a += 1
        if i < n-1 and x < list[i+1]:
            break
        if i == n-1:
            a = n
            break
        i += 1

    print(a)


# AtCorder-Candy Distribution Again

n,x = map(int, input().split())
list[] = list(map(int, input().split()))

a = 0
i = 0
list.sort()

if x < list[0]:
    print(0)

elif x >= list[0]:

    while x >= list[i] and x > 0:
        x -= list[i]
        a += 1
        if i < n-1 and x < list[i+1]:
            break
        if i == n-1:
            if x > list[i]:
                a = n-1
            else:
                a = n
            break
        i += 1

    print(a)

    

# AtCorder-Candy Distribution Again

n,x = map(int, input().split())
list = list(map(int, input().split()))

a = 0
i = 0
list.sort()

if x < list[0]:
    print(0)

elif x >= list[0]:

    while x > 0 and x >= list[i]:
        x -= list[i]
        a += 1
        if x < list[i+1]:
            break
        i += 1

    print(a)


# AtCorder-Candy Distribution Again

n,x = map(int, input().split())
list = list(map(int, input().split()))

a = 0
i = 0
list.sort()

if x < list[0]:
    print(0)

elif x >= list[0]:

    while x > 0 and x >= list[i]:
        x -= list[i]
        a += 1
        if i < n-1 and x < list[i+1]:
            break
        if i == n-1:
            a = n
            break
        i += 1

    print(a)


# AtCorder-Candy Distribution Again

n,x = map(int, input().split())
list = list(map(int, input().split()))

a = 0
list.sort()
i = 0

if x < list[0]:
    print(0)

elif x >= list[0]:
    # x -= list[i]
    # print(x)
    while x >= 0:
        x -= list[i]
        a += 1
        i += 1
        if i == n-1 or x == 0:
            a += 1
            break
    # print(x)
    print(a)


    # return print(a)


n,x = map(int, input().split())
list = list(map(int, input().split()))

a = 0
i = 0

list.sort()

if list[0] > x:
    print(0)

elif x > sum(list):
    print(n-1)

else:
    while x > 0 and x >= list[i]:
        x -= list[i]
        i += 1
        a += 1
        if i == n:
            break

    print(a)

Verdicts: Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Accepted