Problem:


Score : 200 points

Problem Statement

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.

Constraints

All values in input are integers.

2 \leq N \leq 100

1 \leq x \leq 10^9

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.

Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).

Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).

Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).

Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.



Sample input:
3 70
20 30 10


Sample output:
2

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
count = 0
a.sort()

for i in range(N):
    if x == a[i]:
        count += 1
    elif i == N - 1:
        if x != a[i]:
            break
    elif x - a[i] > 0:
        x -= a[i]
        count += 1
    else:
        break

print(count)

Submission status: Accepted

Solution code:
n,x = map(int,input().split())
a = list(map(int,input().split()))
aa = sorted(a)
nokori = x
cnt = 0
if x > sum(a):
         print(n-1)
else:
         for i in range(n):
                  if nokori >= aa[i]:
                           cnt +=1
                           nokori = nokori - aa[i]
         print(cnt)

Submission status: Accepted

Solution code:
N,x,*A=map(int,open(0).read().split());A.sort();s=sum(A)
if s==x:print(N)
else:
    W=[0]
    for a in A[::-1]:W+=[W[-1]+a]
    for i,w in enumerate(W[1:],1):
        if w>=s-x:print(N-i);break

Submission status: Accepted

Solution code:
#import pysnooper
import os,re,sys,operator,math,heapq,string
#from collections import Counter,deque
#from operator import itemgetter
from itertools import accumulate,combinations#,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy

setrecursionlimit(10**6)
input=stdin.readline

n,x=map(int,input().split())
a=sorted([int(i) for i in input().split()])
ans=0
l=0
while x>0 and l<n:
    x-=a[l]
    if x>=0:
        ans+=1
    l+=1
if x>0:
    ans-=1
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N, x = map(int,input().split())
b = sorted(list(map(int,input().split())))
a = np.cumsum(b)
if x == sum(b):
    print(N)
    exit()
for i in range(N):
    if x < a[i]:
        print(i)
        exit()
print(N-1)

Submission status: Accepted

Solution code:
def main():
    n, x = map(int, input().split())
    inix = x
    a = sorted(list(map(int, input().split())))
    if a[0] > x:
        print(0)
        exit()
    elif sum(a) == x:
        print(n)
        exit()
    ans = 0
    for i in range(n):
        if x >= a[i]:
            x -= a[i]
            ans += 1
        else:
            break
    if x != 0 and ans == n:
        ans-=1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, x = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
ans = 0
for a in A:
  if x >= a:
    ans += 1
    x -= a
if ans == N and x != 0:
  ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cnt = 0
for i in a:
  if i > x:
    break
  x -= i
  cnt += 1
else:
  if x > 0:
    cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC27 A

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,x = LI()
a = LI()
a.sort()
ans = 0
for i in range(n):
    if a[i] <= x:
        x -= a[i]
        ans += 1
    else:
        break
if x > 0 and ans == n:
    print(ans-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a_s = input().split()
for i in range(N):
  a_s[i] = int(a_s[i])
  
a_s = sorted(a_s)

ans = 0
sum_ = 0
for i in range(N):
  if (sum_ + a_s[i] <= x)&(i<N-1):
    sum_ += a_s[i]
    ans += 1
  elif (sum_ + a_s[i] == x)&(i==N-1):
    ans += 1
  else:
    break

print(ans)

Submission status: Accepted

Solution code:
N,x=map(int,input().split())
aList=sorted(list(map(int,input().split())))

remain = x
count = 0

if sum(aList)<x:
  count = N-1
elif sum(aList)==x:
  count = N
else:
  for i in range (N):
      if remain-aList[i] >= 0:
        remain-=(aList[i])
        count += 1

print(count)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
c=0
for i in range(n):
  if n>=i and x>=a[i]:
    x-=a[i]
    c+=1
    if n==c and x==0:
      print(n)
    if n==c and x!=0:
      print(n-1)
  else:
    print(c)
    break

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))

A = sorted(A)

ans = 0

for a in A:
    x -= a
    ans += 1
    if x < 0:
        ans -= 1
        break
    if x == 0:
        break
if x > 0:
    ans -= 1
print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = sorted(list(map(int, input().split())))
cnt = 0
for i in A:
  x -= i
  if x < 0:
    print(cnt)
    exit()
  cnt += 1
print(cnt if x == 0 else cnt - 1)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import heapq

N, x = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)
tmp = 0
ctr = 0

for i in range(N):
    tmp += heapq.heappop(a)
    if tmp > x or (a == [] and tmp != x):
        print(ctr)
        exit()
    else:
        ctr += 1
else:
    print(ctr)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))

cnt = 0
A.sort()

for i in range(N-1):
    if A[i] <= x:
        x -= A[i]
        cnt += 1
        
if x == A[-1]:
    cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
n, x = map(int, input().split(" "))
children = list(map(int, input().split(" ")))

children.sort()

for i in range(n):
  x -= children[i]
  if x < 0:
    print(i)
    exit()
    
if x > 0:
  print(n - 1)
elif x == 0:
  print(n)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
arr = map(int, input().split())
count = 0
for m in sorted(arr):
    x -= m 
    if x == 0:
        count += 1
        break
    elif x < 0:
        break
    else:
        count += 1
print(count - 1 if x > 0 else count)

Submission status: Accepted

Solution code:
n,x=map(int, input().split())
a=sorted(list(map(int, input().split())))

for i in range(n):
    x-=a[i]
    if x<0:
        print(i)
        exit()

if x>0:
    print(i)
elif x==0:
    print(i+1)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
c=0
for i in range(n):
  x-=l[i]
  if i+1==n and x!=0:
    break
  if x>=0:
    c+=1
  else:
    break
print(c)

Submission status: Accepted

Solution code:
N,x=[int(i) for i in input().split()]
a = [int(x) for x in input().split()]
a.sort()
cnt=0
for k in a:
	x -= k
	if x < 0:
		break
	cnt +=1
if x >0:
	cnt -=1
print(cnt)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    n,x = LI()
    a = LI()
    a.sort()
    ans = 0
    for i in a:
        if i <= x:
            ans += 1
            x -= i
        else:
            print(ans)
            return
    if x:
        print(ans-1)
    else:
        print(ans)
    return

#B
def B():
    n,X = LI()
    x = LI()
    for i in range(n-1):
        x[i+1] += x[i]
    x.insert(0,0)
    ans = float("inf")
    f = [5]*(n+1)
    for i in range(1,n):
        f[i+1] = f[i]+2
    for k in range(1,n+1):
        i = 0
        a = n
        d = (k+n)*X
        while a > 0:
            d += f[i]*(x[a]-x[max(0,a-k)])
            a -= k
            i += 1
        ans = min(ans,d)
    print(ans)
#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return


#Solve
if __name__ == "__main__":
    A()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, x = map(int, input().split())
        a = list(map(int, input().split()))

        a.sort()

        for i in range(N):
            if x >= a[i]:
                x -= a[i]
                a[i] = 0
            else:
                break
        if x > 0:
            a[-1] += x

        print(a.count(0))


if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))

a.sort()
ans = 0
for i in range(N):
    if i == N - 1:
        if a[i] == x:
            ans += 1
    else:
        if a[i] <= x:
            x -= a[i]
            ans += 1
        else:
            break
print(ans)

Submission status: Accepted

Solution code:
def main():
    N, x = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()
    i = 0
    count = 0
    while i < N and x-A[i]>=0:
        count += 1
        x -= A[i]
        i += 1
    if x == 0:
        return count
    else:
        if i == N:
            return count - 1
        else:
            return count

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = [int(i) for i in input().split()]

a.sort()
temp_x = x
for i in range(n):
  temp_x += -a[i]
  if i == n-1 and temp_x > 0:
    ans = n-1
  elif temp_x > 0:
    continue
  elif temp_x == 0:
    ans = i+1
  else:
    ans = i
    break

print(ans)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
A=0
for i in range(len(a)):
  A += a[i]
if A < x:
  print(n-1)
else:
  b=sorted(a)
  c=0
  ans=0
  for _ in range(len(a)):
    c += b[_]
    if c <= x:
      ans += 1
    else:
      break
  print(ans)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
ans=0
for i in range(n):
    x-=l[i]
    if x<0:
        break
    ans+=1
if x>0:
    ans-=1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

children.sort()

count = 0
index = 0

for c in children:
    # print("{}, {}".format(x, c))
    is_last_child = index + 1 == len(children)
    if is_last_child:
        if x == c:
            x -= c
            count += 1
    elif x >= c:
        x -= c
        count += 1
    index += 1

print(count)

Submission status: Accepted

Solution code:
import sys
N, x = map(int, sys.stdin.readline().rstrip().split())
aa = sorted(list(map(int, sys.stdin.readline().rstrip().split())))
cnt = 0
for a in aa:
    x -= a
    if x < 0: break
    cnt += 1
if x > 0:
    cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
counter = 0
while x > 0 and counter <= len(a)-1:
  if len(a) - 1 > counter and x >= a[counter]:
    x -= a[counter]
    counter += 1
  elif len(a) - 1 == counter and x == a[counter]:
    x = 0
    counter += 1
  else:
    x = 0

print(counter)

Submission status: Accepted

Solution code:
n,x = [int(i) for i in input().split()]
array = [int(i) for i in input().split()]

y=x

array.sort()

cnt = 0
for i in range(n):
  x -= array[i]
  if x >= 0:
    cnt += 1
    
if cnt == n and sum(array) !=y:
  cnt = cnt-1
    
print(cnt)

Submission status: Accepted

Solution code:
nx = input().split()
n = int(nx[0])
x = int(nx[1])
a = input().split()
for i in range(n):
	a[i] = int(a[i])

a = sorted(a)
c = 0
for i in range(n-1):
	x = x - a[i]
	c += 1
	if x < 0:
		c -= 1
		break

if x == a[n-1]:
	c += 1
	
print(c)

Submission status: Accepted

Solution code:
n,x = map(int,input().split())
a = list(sorted(list(map(int,input().split()))))
ans = 0
for i in range(n+1):
  if i == n:
    if x > 0:
      ans -= 1
  elif x > a[i]:
    x -= a[i]
    ans += 1
  elif x == a[i]:
    ans += 1
    x -= a[i]
  else:
    break
print(ans)

Submission status: Accepted

Solution code:
import itertools

N, x = [int(y) for y in input().split(' ')]
arr = sorted([int(y) for y in input().split(' ')], reverse=False)
cumsum = list(itertools.accumulate(arr))

ret = 0
p = True

for csum in cumsum:
  if csum > x:
    print(ret)
    p = False
    break
    
  ret += 1

if p: 
  print(ret) if cumsum[len(cumsum) - 1] == x else print(ret - 1)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

ans = 0
for i in range(N):
    if a[i] <= x:
        ans += 1
        x -= a[i]
        if i == N - 1 and x != 0:
            ans -= 1
    
print (ans)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
kids=list(map(int,input().split()))
kids.sort()
count=0
for i in range(n):
    x=x-kids[i]
    if x<0:
        break
    count+=1

if x>0:
    print(count-1)
else:
    print(count)

Submission status: Accepted

Solution code:
n,x=list(map(int,input().split()))
a=list(map(int,input().split()))

a.sort()
count=0
for i in range(n):
  x=x-a[i]
  if(x<0):
    break;
  count=count+1
if(x>0):
  count=count-1
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools, collections, copy, heapq


def solve(N: int, x: int, a: "List[int]"):
    a.sort()
    res = 0
    xx = x
    for i in range(N):
        if x >= a[i]:
            res += 1
            x -= a[i]
        else:
            x = 0
    if x > 0:
        res -= 1
    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    x = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, x, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
_, X = map(int, input().split())
A = sorted(list(map(int, input().split())))

count = 0
for a in A:
    if X - a >= 0:
        X -= a
        count += 1

if count == len(A) and X > 0:
    count -= 1

print(count)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(sorted(map(int, input().split())))
if x == sum(a):
    print(n)
else:
    acc = 0
    for i in range(n):
        acc += a[i]
        if acc > x:
            break
    if i == n:
        print(n - 1)
    else:
        print(i)

Submission status: Accepted

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
sum_a_list = [a[0]]
for i in range(1,N):
    sum_a_list.append(sum_a_list[i-1]+a[i])
if sum(a) < x:
    print(N-1)
elif sum(a) == x:
    print(N)
else:
    for i in range(N):
        if sum_a_list[i] > x:
            print(i)
            break

Submission status: Accepted

Solution code:
from itertools import accumulate
n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a_acc = list(accumulate(a))
for i in range(n):
    if a_acc[i] > x:
        print(i)
        exit()
if sum(a) == x:
    print(n)
else:
    print(n-1)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
ans = 0
looped = True
for a in A:
    if x < a:
        looped = False
        break
    x -= a
    ans += 1

if looped and x > 0 and ans > 0:
    ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N, X = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
result = 0

for i in range(N):
    X -= A[i]
    if X <= 0:
        if X == 0:
            result = i+1
        else:
            result = i
        break

if X > 0:
    result = N-1

print(result)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = [int(e) for e in input().split()]
a.sort()
result = 0
for i in a:
  x -= i
  if x < 0:
    break
  result += 1
if x != 0 and result == n:
  result -= 1
print(result)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = sorted(list(map(int, input().split())))

count = 0
for i in range(N-1):
  if x>= a[i]:
    x = x - a[i]
    count += 1

if x == a[N-1]:
  count += 1
    
    
print(count)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline

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

if sum(a) == x:
    print(n)
else:
    a.sort()
    s = 0
    for i,v in enumerate(a):
        s += v
        if s > x:
            print(i)
            exit()
    print(n-1)

Submission status: Accepted

Solution code:
mans, nums = map(int,input().split())
req_array  = sorted(list(map(int,input().split())))
count = 0

for i in range(mans):

    if ( i +1 ) == mans and nums == req_array[i]:
        count += 1
        nums -= req_array[i]
    elif ( i +1 ) == mans:
        break

    if nums >= req_array[i]:
        count += 1
        nums -= req_array[i]
    else:
        break
print(count)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
an = list(map(int, input().split()))
c = 0
for i in sorted(an):
    if x >= i:
        x -= i
        c += 1
    else:
        print(c)
        exit()
if x > 0:
    print(c - 1)
else:
    print(c)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())

al=list(map(int,input().split()))

al.sort()

count=0

for i in range(n) :
  d=x-al[i]
  if d<0 :
    break
  
  if i==n-1 and d>0 :
    break
  
  x=x-al[i]
  #print(x)
  count+=1
print(count)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = sorted(list(map(int, input().split())))

for i in range(N):

    x -= a[i]
    if x < 0:
        print(i)
        break    
else:
    if x == 0:
        print(N)
    else:
        print(N-1)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
ans=0
for i,a in enumerate(A):
    if i<n-1:
        if x>=a:
            ans+=1
            x-=a
        else:
            break
    else:
        if x==a:
            ans+=1
        else:
            break
print(ans)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
l = list(map(int, input().split()))
l.sort()

i = 0
ans = 0
while x > 0 and i<= n-1:
  if x >= l[i]:
    x -= l[i]
    if i == n-1:
      if x == 0:
        ans += 1
        break
      else:
        break
    else:
      i += 1
      ans += 1
  else:
    x = 0
    break
print(ans)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cnt = 0
for i in a:
    if i <= x:
        x -= i
        cnt += 1
    else:
        print(cnt)
        exit()
print(cnt if x == 0 else cnt - 1)

Submission status: Accepted

Solution code:
#AGC027A
n,x = list(map(int,input().split()))
a = list(map(int,input().split()))
a.sort()
k = 0
for i in range(n-1):
    if a[i] <= x:
        k += 1
        x -= a[i]
if a[n-1] == x:
    k += 1
print(k)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
if sum(a) == x:
  print(N)
else:
  while sum(a) > x:
    a.remove(max(a))
  l = len(a)
  if l == N:
    print(l - 1)
  else:
    print(l)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
happy_lists = sorted([int(i) for i in input().split()])
happy_children = 0

for _ in range(n):
    if x >= happy_lists[_]:
        if _ == len(happy_lists)-1:
            if x == happy_lists[_]:
                happy_children += 1
        else:
            happy_children += 1
            x -= happy_lists[_]

print(happy_children)

Submission status: Accepted

Solution code:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
N, X = map(int, input().split())
a = sorted(list(map(int, input().split())))
count = 0
for i in range(N):
    if X >= a[i]:
        count += 1
        X = X - a[i]
    else:
        break
if count == N and X > 0:
    count -= 1  # 全員に配りきってお菓子が余った場合、一人喜ばない子供が出る。
print(str(count))

Submission status: Accepted

Solution code:
N,x,*a = map(int, open(0).read().split())

if sum(a) == x:
  print(N)
  exit()
  
cnt = 0
for i, a_ in enumerate(sorted(a)):
  x -= a_
  if 0 <= x:
    cnt += 1
  else:
    break
print([cnt,cnt-1][0<x])

Submission status: Accepted

Solution code:
N,x,*a = map(int, open(0).read().split())
cnt = 0
for i, a_ in enumerate(sorted(a)):
  x -= a_
  if not i == len(a)-1:
    if 0 <= x:
      cnt += 1
    else:
      break
  else:
    if x == 0:
      cnt += 1
    else:
      break
print(cnt)

Submission status: Accepted

Solution code:
n, x = map(int, input().rstrip("\n").split())
kids_needs = sorted(list(map(int, input().rstrip("\n").split())))

c = 0
for need in kids_needs:
    x -= need
    if x < 0:
        break
    c += 1
else:
    c -= bool(x)

print(c)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())

a_list = list(map(int, input().split()))
a_list.sort()
give = 0
count = 0

for num in a_list:
    give += num
    if give == x:
        count += 1
        break
    if give > x:
        count -= 1
    count += 1

if give < x:
    count -= 1

print(count)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))

a_sort = sorted(a)

for i in range(N):
    x -= a_sort[i]
    if x < 0 or (x > 0 and i == N-1):
        print(i)
        break
    elif x == 0:
        print(i + 1)
        break

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


N, x = map(int, input().split())
a = list(map(int, input().split()))

memolist = [0]*(N+1)

import copy

for child in a:
    nextmemolist = copy.copy(memolist)
    for i in range(len(memolist)):
        if (memolist[i] + child) <= x and ((memolist[i] + child) < memolist[i+1] or memolist[i+1] == 0):
            nextmemolist[i+1] = memolist[i] + child
        if memolist[i+1] == 0:
            break
    memolist = nextmemolist


if memolist[-1] == x:
    print(N)
elif memolist[-1] != 0:
    print(N-1)
else:
    for i, ans in enumerate(memolist):
        if i == 0:
            continue
        if ans == 0:
            print(i-1)
            break

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect

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

arr.sort()
sum_arr = list(accumulate(arr))

idx = bisect(sum_arr, x)

if idx == n and sum_arr[n - 1] != x:
    idx -= 1

print(idx)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a_list = list(map(int, input().split()))
a_list = sorted(a_list)

count = 0
for i in range(N):
  if x - sum(a_list[:i]) > 0:
    count += 1
  else:
    break
if x - sum(a_list[:count]) != 0:
  count += -1
print(count)

Submission status: Accepted

Solution code:
#%%
N, x = map(int, input().split())
a = list(map(int, input().split()))

a.sort()

b = 0
count = 0

for i in range(N):
    if i != N-1:
        if x - b >= a[i] :
            count += 1
        else:
            break
    else:
        if x - b == a[i] :
            count += 1
        
    b += a[i]

print(count)

Submission status: Accepted

Solution code:
from bisect import bisect
from itertools import accumulate
n,x,*a=map(int,open(0).read().split())
b=list(accumulate(sorted(a)))
print(bisect(b,x)-(b[-1]<x))

Submission status: Accepted

Solution code:
N,x=map(int,input().split())
A=list(map(int,input().split()))
AA=sorted(A)
ans=0
for i in range(N):
  if x>=AA[i]:
    x-=AA[i]
    ans+=1
  else:
    x=0
    break
if x>0:
  print(ans-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
N, x = map(int,input().split())
count = 0
arr = sorted(list(map(int,input().split())))
for i in range(N):
  x -= arr[i]
  if x > 0:
    count += 1
  elif x == 0:
    count += 1
    break
  else:
    break
if x > 0:
  print(count-1)
else:
  print(count)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = sorted(list(map(int,input().split())))

if x == sum(a):
  print(n)
  exit()

else:
  c = 0
  for i in a:
    x -= i
    c += 1
    if x < 0:
      break
  print(c-1)

Submission status: Accepted

Solution code:
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
cnt = 0
for i in range(n):
	x -= a[i]
	if x >= 0:
		cnt += 1
	else:
		break
if x > 0:
	cnt -= 1
print(cnt)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from copy import *

n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 0
for b in a:
    if x >= b:
        ans += 1
        x -= b
    else:
        print(ans)
        exit()
print(ans-(x>0))

Submission status: Accepted

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))

a.sort()


for i in range(N):
    if x<a[i]:
        print(i)
        exit()
    elif x == a[i]:
        print(i+1)
        exit()
    else:
        x = x - a[i]
    
print(N-1)

Submission status: Accepted

Solution code:
# AGC027A - Candy Distribution Again
# greedy algorithm
from itertools import accumulate


def main():
    # maximize the number of proper distribution -> begin with small values
    n, x = tuple(map(int, input().rstrip().split()))
    A = sorted(map(int, input().rstrip().split()))
    ac = list(accumulate(A))
    if ac[-1] < x:
        ans = n - 1
    else:
        ans = sum(i <= x for i in ac)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
n,x=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
b=[a[0]]
for i in range(1,n):
  b.append(b[i-1]+a[i])
if sum(a)==x:
  print(n)
elif sum(a)<x:
  print(n-1)
else:
  print(bisect.bisect(b,x))

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0

a.sort()

for i in range(N):
    x -= a[i]
    if x >= 0:
        ans += 1

if x>0:
    print(ans-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
N,x = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
cnt =0
for i in range(N-1):
    if x<A[i]:
        break
    else:
        x = x-A[i]
        cnt += 1

if x == A[N-1]:
    cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c = list(map(int, input().split()))
c.sort()
d = 0
for i in c:
    b -= i
    d += 1
    if b < 0:
        break
if b != 0:
    d -= 1
print(d)

Submission status: Accepted

Solution code:
N,x=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
ans=0
k=0
for i in a:
        if k != N-1:
                if x >= i:
                        x-=i
                        ans+=1
                else:break
        elif x==i:ans+=1
        k+=1
print(ans)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
l=sorted(list(map(int,input().split())))
ans=0
for i in l:
    x-=i
    if x>=0:ans+=1
if x>0:ans-=1
print(ans)

Submission status: Accepted

Solution code:
N,x = map(int, input().split())
a= [int(i) for i in input().split()]

a_s = sorted(a)

count=0
if sum(a) < x:
  count = -1
for i in range(N):
  if a_s[i] <= x:
    count += 1
    x -= a_s[i]
print(count)

Submission status: Accepted

Solution code:
N,x = map(int, input().split())
a= [int(i) for i in input().split()]

a_s = sorted(a)

count=0
if sum(a) < x:
  count = -1
for i in range(N):
  if a_s[i] <= x:
    count += 1
    x -= a_s[i]
  else:
    break
print(count)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
al=list(map(int,input().split()))
al=sorted(al)
ans=0
for a in al:
    if a>x:
        break
    x-=a
    ans+=1
else:
    if x>0 and ans>0:
        ans-=1
print(ans)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
count = 0
remain = x
a.sort()

for i in range(len(a)):
    if sum(a) == x:
        while n > 0:
            count += 1
            n -= 1
        continue
    remain -= a[i]
    if remain >= 0 and i != len(a)-1:
        count += 1
    else:
        break

print(count)

Submission status: Accepted

Solution code:
N,x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a_sorted = sorted(a)
num =0
x_num = x
for i in range(N):
    if(x_num - a_sorted[i] >= 0):
        x_num -= a_sorted[i]
        num += 1
        if(i == N-1 and x_num >0):
            num -= 1
    else:
        break
print(num)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c = list(map(int, input().split()))
c.sort()
e = 0
f = 0
for i in c:
    e += i
    f += 1
    if e >= b:
        break
if e != b:
    f -= 1
print(f)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = sorted(list(map(int, input().split())))

count = 0
for i in range(N):
    if x >= a[i]:
        if i != N-1:
            x -= a[i]
            count += 1
        else:
            if x == a[i]:
                count += 1
    else:
        break
print(count)

Submission status: Accepted

Solution code:
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
    
n,x=MI()
a=LI()
a.sort()
y=x
ans,s=0,0
for i in range(n):
    s+=a[i]
    if s>x:
        break
    else:
        if i<n-1:
            ans+=1
        elif x-s==0:
            ans+=1
            
print(ans)

Submission status: Accepted

Solution code:
N,x=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
y=x
ans=0
for i in range(N-1):
    if a[i]<=y:
        ans+=1
        y-=a[i]
    else:
        break
if a[N-1]==y:
    ans+=1
print(ans)