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())

childlen = []
for part in input().split():
    childlen.append(int(part))

childlen.sort()

happy = 0
for n in range(len(childlen)):
    child = childlen[n]
    x -= child
    happy += 1
    
    if x < 0:
        print(happy - 1)
        exit(0)
    elif x == 0:
        print(happy)
        exit(0)

# もしも配りきれなかったら、一旦戻す
if x != 0:
    happy -= 1

print(happy)

Submission status: Accepted

Solution code:
hito, ame = map(int, input().split())
NeedAme = list(map(int, input().split()))
NeedAme.sort()

ans = 0
total = 0
for i in range(0, hito):
    if ((i != hito - 1) and (total + NeedAme[i] <= ame)) or ((i == hito - 1) and (total + NeedAme[i] == ame )):
        ans += 1
        total += NeedAme[i]
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
#agc027a
import sys

n,x=map(int,raw_input().split())
a=map(int,raw_input().split())
a.sort()
b=[]
b.append(a[0])
for i in xrange(1,n):
 b.append(b[i-1]+a[i])
for i in xrange(n):
 if b[i]==x:
  print i+1
  sys.exit()
 elif b[i]>x:
  print i
  sys.exit()
print n-1

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = sorted([int(i) for i in input().split()])
b = []
for i, v in enumerate(a):
    if i != N - 1:
        c = min(v, x)
    elif x > v:
        c = x
    else:
        c = min(v, x)
    b.append(c)
    x -= c
ans = 0
for i,j in zip(a,b):
    if i == j:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
input_lines = input().split()
N = int(input_lines[0])
x = int(input_lines[1])
input_lines = input().split()
list_a = []
for i in range(N):
    list_a.append(int(input_lines[i]))

list_a.sort()
max = 0
for i in range(N):
    current_candy = x
    current_child = 0
    for j in range(i,N):
        tmp = current_candy - list_a[j]
        if tmp >= 0:
            current_candy = tmp
            current_child += 1
    if tmp > 0:
        current_child -= 1
    if current_child >= max:
        max = current_child

print(max)

Submission status: Accepted

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
left=0
right=0
s=a[0]
answer = 0
while True:
    if left >= N or right >= N:
        break
    if s <= x:
        if s == x:
            answer = max(answer,right-left+1)
        if s < x:
            if right-left < N-1:
                answer = max(answer,right-left+1)
        right += 1
        if right < N:
            s += a[right]
        continue
    if s > x:
        s -= a[left]
        left += 1
        continue
print(answer)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
n, x = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort()
cnt = 0
flag = 0
for a in a_list:
    if x >= a:
        cnt += 1
        x -= a
    else:
        flag = 1
        break
if flag == 0 and x > 0:
    cnt -= 1
print(max(cnt, 0))

Submission status: Accepted

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

ans = 0

sumA = sum(A)
A.sort()

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

print(ans)

Submission status: Accepted

Solution code:
s=input().split()
n=int(s[0])
x=int(s[1])
s=input().split()
l=[int(c) for c in s]
l.sort()
i=0
cnt=0
while True:
    if x>=l[i]:
        x-=l[i]
        cnt+=1
    else:
        x-=l[i]
        break
    i+=1
    if i==len(l):
        break
if x>0:
    cnt-=1
print(cnt)

Submission status: Accepted

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


def main():
    from bisect import bisect
    from itertools import accumulate

    n, x = map(int, input().split())
    a = sorted([0] + list(map(int, input().split())))
    summed_a = list(accumulate(a))
    count = bisect(summed_a, x)

    if count >= n + 1:
        count -= 1

    if summed_a[count] == x:
        print(count)
    else:
        print(count - 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = sorted([int(x) for x in input().split()])
ans = 0
for i in range(N):
    if x < a[i]:
        break
    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())
a=list(map(int, input().split()))
a.sort()
k,i=0,0
ans=0
while i<n:
	k+=a[i]
	ans+=1
	if k>x:
		ans-=1
		break
	i+=1
if k<x:
	ans-=1
print(ans)

Submission status: Accepted

Solution code:
N, X = map(int, raw_input().split())
A = map(int, raw_input().split())
A.sort()
ans = 0

for i in xrange(N):
    if X >= A[i]:
        if i < N - 1 or X == A[i]:
            ans += 1
            X -= A[i]

print ans

Submission status: Accepted

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

SA = sum(A)
if x == SA:
    print(N)

else:
    AS = sorted(A)
    kk = 0
    for k, a in enumerate(AS):
        if x < a:
            break
        x -= a
        kk = k + 1
    if kk == N: 
        kk = N - 1
    print(kk)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = sorted(map(int, input().split()))
for i in range(0, n - 1):
  k = min(a[i], x)
  a[i] -= k
  x -= k
a[-1] -= x
print(a.count(0))

Submission status: Accepted

Solution code:
def main():
    """
    2 <= N <= 100
    1 <= ai,x <= 10^9
    """
    N, x = map(int, input().split())
    *a, = map(int, input().split())

    ans = 0
    # 喜ぶようにちょうど配る
    for _a in sorted(a):
        if _a <= x:
            ans += 1
            x -= _a

    # ちょうど配るには多かった
    # ans < N であれば、喜んでいないだれかに押し付ける
    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 = sorted(list(map(int,input().split())))

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

print(ans)

Submission status: Accepted

Solution code:
N, V = list(map(int, input().split()))
C = list(map(int, input().split()))
N = len(C)

if sum(C)==V:
  print(N)
else:
  cnt = 0
  for c in sorted(C):
    if V-c>0:
      cnt += 1
      V -= c
    elif V-c==0:
      cnt += 1
      break
    else:
      break
  print(min(cnt, N-1))

Submission status: Accepted

Solution code:
def cda(N, x, s, count):
    if N > 0 and x < s[0]:
        return count
    if N == 0:
        if x > 0:
            return count - 1
        else:
            return count
    else:
        return cda(N - 1, x - s[0], s[1:], count + 1)

def agc027a():
    N, x = (int(s) for s in input().split(" "))
    a = [int(s) for s in input().split(" ")]
    answer = cda(N, x, sorted(a), 0)
    print(answer)

def main():
    agc027a()

main()

Submission status: Accepted

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

a.sort()

ans = 0
for ai in a:
    if x >= ai:
        x -= ai
        ans += 1
    else:
        break
else:
  if x:
    ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))
 
x_tmp = x
for i, Ai in enumerate(sorted(A)):
    x_tmp -= Ai
    if x_tmp == 0:
        ans = i + 1
        break
    elif x_tmp < 0:
        ans = i
        break
    else: continue
else: ans = i
 
print(ans)

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-1):
  x=x-a[i]
  if x>=0:c+=1
if x==a[n-1]:c+=1
print(c)

Submission status: Accepted

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

b = []
for _ in range(N):
    b.append(0)
    
c = 0

for i in range(N):
    for j in range(i+1):
        b[i] += a[j]

for k in range(N-1):
    if b[k] <= X:
        c += 1
if b[N-1] == X:
    c += 1
print(c)

Submission status: Accepted

Solution code:
# encoding: utf-8
# N = int(input())
# S = input()
# N, M = map(int, input().split())
# A = list(map(int, input().split()))

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

x_tmp = x
for i, Ai in enumerate(sorted(A)):
    x_tmp -= Ai
    if x_tmp == 0:
        ans = i + 1
        break
    elif x_tmp < 0:
        ans = i
        break
    else: continue
else: ans = i

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def a_candy_distribution_again(N, X, A):
    tmp = 0  # お菓子を要求された量以上配れる子供の人数
    for a in sorted(A):
        X -= a
        if X < 0:
            break
        tmp += 1
    ans = tmp - int(X > 0)  # お菓子に端数が出た場合、1人喜ばせられない
    return ans

N, X = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
print(a_candy_distribution_again(N, X, A))

Submission status: Accepted

Solution code:
import sys

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


a.sort()
for i, ai in enumerate(a):
    x -= ai
    if x < 0:
        print(i)
        sys.exit()

if x == 0:
    print(N)
else:
    print(N-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 23:36:39 2018

@author: Yuki
"""

N, x = map(int, input().split())
A = sorted([int(i) for i in input().split()])


cnt=0
res=0
for i in range(0,N):
    cnt += A[i]
    if cnt > x:
        res = i
        break
    elif cnt == x:
        res = i+1
        break
    if i == N-1 and cnt < x:
        res = N-1
if A[0] > x:
    res=0
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
l = sorted(list(map(int, input().split())))
ans = 0
sum = 0
for i, v in enumerate(l):
  sum += v

  if i == (n-1) and sum < x:
    break

  if sum <= x:
    ans += 1
  else:
    break
    
print(ans)

Submission status: Accepted

Solution code:
n,x,*a=map(int,open(0).read().split())
c=0
for t in sorted(a):
  x-=t
  if x<0:break
  c+=1
print(c-(x>0))

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
c=0
for t in sorted(map(int,input().split())):
  x-=t
  if x<0:break
  c+=1
print(c-(x>0))

Submission status: Accepted

Solution code:
n,x,*a=map(int,open(0).read().split())
a.sort()
if sum(a)<x:a=a[::-1]
c=0
for t in a:
  x-=t
  if x<0:break
  c+=1
print(c-(x>0))

Submission status: Accepted

Solution code:
import numpy

N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
a_sum = numpy.cumsum(a)
ans = numpy.sum(a_sum <= x)

if ans == N and x not in a_sum:
    ans -= 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def run(n, x, a):
    a = sorted(a)
    cnt = 0
    for i in range(n):
        x -= a[i]
        if x >= 0:
            cnt += 1
    if x > 0:
        cnt -= 1
    return cnt


def main():
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    print(run(n, x, a))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, x=map(int,input().split())
a=[int(i) for i in input().split()]
a.sort()
j=1
S=a[0]
while (S<x)and(j<N):
  S=S+a[j]
  j=j+1
if S==x:
  print(j)
else:
  print(j-1)

Submission status: Accepted

Solution code:
a = input().split()
a = [int(i) for i in a]
b = input().split()
b = [int(i) for i in b]
b = sorted(b)
output = 0
end = False

for i in range(len(b)-  1):
	if a[1] - b[i] > 0:
		a[1] += -b[i]
		output += 1
	elif(a[1] - b[i] == 0):
		output += 1
		end = True
		break
	else:
		end = True
		break
		
if end == False:
	if a[1] - b[-1] == 0:
		output += 1
		 
print(output)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
dp = [[]]
a.sort()
ans = 0
f = False
for b in a:
    if b <= x:
        ans += 1
        x -= b
        f = True
    else:
        f = False
if x > 0 and f:
    ans -= 1
print(ans)

Submission status: Accepted

Solution code:
in1 = input()
in2 = input()
N, x = in1.split()
N = int(N)
x = int(x)
a = list()
for i in in2.split():
  a.append(int(i))
a.sort()
count = 0
for ai in a:
  if x > ai:
    count += 1
    x -= ai
  elif x == ai:
    count += 1
    x -= ai
    break
  else:
    x -= ai
    break
if x > 0 and count > 0:
  count -= 1
print(count)

Submission status: Accepted

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from string import *
from itertools import *

N,X = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
happy = 0
for i in range(len(l)):
	if(X-l[i] == 0):
		X -= l[i]
		happy += 1
	elif(X-l[i] > 0 and i!=len(l)-1):
		X -= l[i]
		happy += 1
	else:
		break
print(happy)

Submission status: Accepted

Solution code:
def main():
    n, x = [int(s) for s in input().split()]
    a = [int(s) for s in input().split()]
    a.sort()

    cnt = 0
    for i, y in enumerate(a):
        if x < y:
            break
        x -= y
        cnt += 1
    else:
        # distribute to all child
        if x != 0:
            cnt -= 1
    print(cnt)

main()

Submission status: Accepted

Solution code:
def main():
    N, x = map(int, input().split())
    A = sorted([int(i) for i in input().split()])
    a_sum = sum(A)
    if x == a_sum:
        print(N)
    elif x > a_sum:
        print( N-1 )
    else:
        cnt = 0
        for a in A:
            if x >= a:
                cnt += 1
                x -= a
            else:
                print(cnt)
                return 0

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

N, x = map(int, raw_input().split())

a = map(int, raw_input().split())

a.sort()

ans = 0

for num in a:
    x -= num
    if x < 0:
        break
    ans += 1

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

print ans

Submission status: Accepted

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

if len(child_list) == 1:
    if x > min(child_list):
        please_child = 0
    elif x == min(child_list):
        please_child = 1
elif len(child_list) > 1:
    nokori = x - min(child_list)
    please_child = 0 
    while nokori >=0:
        please_child +=1
        mini = min(child_list)
        child_list.remove(mini)
        if child_list == []:
            break
        if len(child_list) == 1:
            if nokori == min(child_list):
                please_child += 1
            if nokori > min(child_list):
                break
            else:
            	break
        else:    
            nokori -= min(child_list)
print(please_child)

Submission status: Accepted

Solution code:
n = [int(i) for i in input().split()]
child = n[0]
candy = n[1]

distribute_candy = sorted([int(i) for i in input().split()])
distribute_candy.sort()

delighted_child = 0

for j in range(child):
    if child - 1 == j:
        if distribute_candy[j] == candy:
            delighted_child += 1
            break
        else:
            break
    if candy - distribute_candy[j] >= 0:
        delighted_child += 1
        candy = candy - distribute_candy[j]

print(delighted_child)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# cook your dish here
n,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
ass = 0
while len(a)>= 1:
    if a[0]<= x and len(a)>= 2:
        x = x -a[0]
        ass += 1
        del a[0]
    elif a[0] == x:
        ass += 1
        del a[0]
    else:
        break
print(ass)

Submission status: Accepted

Solution code:
N,x = map(int,input().split())
a_list = list(map(int,input().split()))
a_list_sort = sorted(a_list)
#print(a_list)
#print(a_list_sort)
count = 0

if sum(a_list) < x :
	count = N-1
elif sum(a_list) == x :
	count = N
else:
	for i in range(N):
		if sum(a_list_sort[:i+1]) <= x:
			continue	
		else:
			count = i
			break
print(count)

Submission status: Accepted

Solution code:
def cand(amari, a, count):
    #print("first", amari, a, count)
    if len(a)!=0:
        ma=min(a)
        a.remove(ma)
    else:
        print(count)
        exit()
    if amari>=ma:
        amari-=ma
        count+=1
        if len(a)==0 and amari!=0:
            count-=1
        #print("111", amari, ma, a, count)
        cand(amari, a, count)
    else:
        cand(amari, a, count)




[n, x]=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
count=0
cand(x, a, count)

Submission status: Accepted

Solution code:
N = 2 # 入力は2行
for i in range(N):
    line = input() # 入力を1行受け取る
    if i == 0: # 1行目
        child_count, candy_sum_orig = map(int, line.split())
    else: # 2行目
        requirement_list = list(map(int, line.split()))
satisfied_count = 0
candy_sum = candy_sum_orig
requirement_list.sort() # 昇順に並び替え
for i in range(child_count): # len(requirement_list) == child_count
    requirement = requirement_list[i]
    # print(i, candy_sum, requirement)
    if i == child_count-1: # 最後の人(残りのお菓子がほしい数と一致するか)
        if requirement == candy_sum:
            satisfied_count += 1
    else: # 1人目からchild_count-1人目まで
        if requirement <= candy_sum:
            candy_sum -= requirement
            satisfied_count += 1
count_by_asc = satisfied_count

satisfied_count = 0
candy_sum = candy_sum_orig
requirement_list.reverse() # 降順に並び替え
for i in range(child_count): # len(requirement_list) == child_count
    requirement = requirement_list[i]
    # print(i, candy_sum, requirement)
    if i == child_count-1: # 最後の人(残りのお菓子がほしい数と一致するか)
        if requirement == candy_sum:
            satisfied_count += 1
    else: # 1人目からchild_count-1人目まで
        if requirement <= candy_sum:
            candy_sum -= requirement
            satisfied_count += 1
# print(satisfied_count, count_by_asc)
print(max(satisfied_count, count_by_asc))

Submission status: Accepted

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

ch = 0 # 満足した子供の数
s = 0
for i in range(n):
    if x < a[i]:
        x = 0
    elif x >= a[i]:
        x -= a[i]
        ch += 1
        
if x > 0:
    ch = ch -1

print(ch)

Submission status: Accepted

Solution code:
def solver(N, x, requests):
    count = 0
    list.sort(requests)
    for req in requests:
        if x < req or x == 0:
            count += 1
            break
        x -= req
        N -= 1
        count += 1
    if N > 0 or (x > 0 and count > 0):
        count -= 1
    return count
N, x = tuple([int(n) for n in input().split(" ")])
requests = [int(n) for n in input().split(" ")]
print(solver(N, x, requests))

Submission status: Accepted

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

A = sorted(A)

def solve(a, x, j):
    cnt = 0
    f = False
    for i in range(j, n):
        if x >= a[i]:
            x -= a[i]
            cnt += 1
        else:
            cnt += 1
            f = True
        if f or x == 0:
            break
    if x == 0:
        return cnt
    else:
        return cnt - 1
    
res = 0
for i in range(n):
    res = max(res, solve(A[::-1], X, i))
    res = max(res, solve(A, X, i))
print(res)

Submission status: Accepted

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

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

low = 0
high = N - 1
most_happy = 0

while low <= high:
    i = int((low + high) / 2)
    if sum(a[0:i]) <= x:
        low = i + 1
        if i > most_happy:
            most_happy = i
    else:
        high = i - 1

print(most_happy)
exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n, x = list(map(int, input().split())) # 整数
array = list(map(int, input().strip().split()))
array.sort()
ans = 0
l = len(array)
for i in array:
    l -= 1
    x = x-i
    if l == 0:
        if x != 0:
            break
            
    if x < 0:
        break
    ans += 1
    
print(ans)

Submission status: Accepted

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

y = x
cnt = 0
for ai in a:
    if y - ai >= 0:
        y -= ai
        cnt += 1
if y and cnt == n:
    print(cnt - 1)
else:
    print(cnt)

Submission status: Accepted

Solution code:
nx = input().split()
an = input().split()

n = int(nx[0])
x = int(nx[1])

count = 0
an = list(map(int, an))
an.sort()
num = len(an) - 1
for i in range(len(an)):
    if (x >= an[i]):
        count += 1
        x = x - an[i]
        if (i == num):
            if (x != 0):
                count -= 1
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(a)
b = [0 for i in range(n)]
count = 0
for i in range(n):
    if x - a[i] == 0:
        count += 1
        x -= a[i]
        b[i] = 1
    elif x - a[i] > 0 and i != n-1:
        count += 1
        x -= a[i]
        b[i] = 1
    """
    else:
        if x != 0 and i == n-1:
            if count > 0:
                print(count-1)
                print("aaaaa")
                exit()
            else:
                print(0)
                exit()
        elif x == 0:
            print(count)
            exit()
        else:
            print(count)
        exit()
    print(b)
    """
print(count)

Submission status: Accepted

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


total = sum(a)
num = 0

if total == x:
    num = N
elif total < x:
    num = N -1
else:
    a.sort()
    cum = 0
    for i, aa in enumerate(a):
        cum += aa
        if cum > x:
            num = i
            break

print(num)

Submission status: Accepted

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

al.sort()

cum = [sum(al[:i+1])for i in range(len(al))]

ans = N-1
for i in range(len(al)):
    if cum[i] == x:
        ans = i+1
        break
    elif cum[i]>x:
        ans = i
        break
print(ans)

Submission status: Accepted

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

for i in range(n):
    if i == n-1:
        if a[i] == x:
            res += 1
        else:
            break
    elif a[i] <= x:
        x -= a[i]
        res += 1

print(res)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = [int(i) for i in input().split()]
A.sort()
sums = 0
for i in range(N):
    sums += A[i]
    if sums > x:
        print(i)
        exit()
else:
    if sums == x:
        print(N)
    else:
        print(N - 1)

Submission status: Accepted

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

def f(a, x):
    if not a:
        return 0

    a.append(x)
    a = sorted(a)
    i = a.index(x)
    if i < len(a)-1:
        return len(a) - 2
    elif a[i-1] == a[i]:
        return len(a) - 2
    return f(a[:i-1], x-a[i-1])


s = sum(a)
if x == s:
    print(N)
elif x > s:
    print(N-1)
else:
    print(f(a, s-x))

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] > 0:
        x = x - a[i]
    elif x - a[i] == 0:
        i += 1
    else:
        break
print(i)

Submission status: Accepted

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

a.sort()
y = 0
j = 0
for i in a:
    y += i
    if y > x: break
    j += 1
if y < x: j -= 1
print(j)

Submission status: Accepted

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

N = l1[0]
x = l1[1]

i = 0
s = 0

A.sort()

while True:
    if x < A[i]:
        break
    if i == N-1:
        if A[i] == x:
            s = s + 1
        break
    x = x - A[i]
    i = i + 1
    s = s + 1


print(s)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
if x < min(a):
    # print('x < min(a)')
    print(0)
    exit()
res = 0  # 喜ぶ子供の人数
cnt = 0  # お菓子を配った子供の数
for i in range(len(a)):
    if x >= a[i]:
        # お菓子を配る
        res += 1
        x -= a[i]
        cnt += 1
        # print('a[i], x = ', a[i], x)
    else:
        break
if x > 0:
    # print('x > 0')
    # まだもらっていない子供がいるならその子供に配る
    # そうでないなら既にもらった子供に配る-->喜ぶ子供が1人減る
    if cnt >= n:
        # print('cnt >= n')
        res -= 1
print(res)

Submission status: Accepted

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

def func():
    cummulative_sum = 0
    for index, value in enumerate(A):
        cummulative_sum += value
        if cummulative_sum > x:
            return index

    if cummulative_sum != x:
        return len(A) - 1
    else:
        return len(A)

ans = func()

print(ans)

Submission status: Accepted

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

a = list(map(int, input().split()))
a.sort()

def distributeCandy(a):

    stock = int(x)
    result = 0

    for i in a:
        if stock < i:
            print(result)
            return

        if stock == 0:
            result += 1
            print(result)
            return

        result += 1
        stock -= i

    if stock == 0:
        print(result)
    else:
        print(result - 1)

distributeCandy(a)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N, x = [int(x) for x in stdin.readline().rstrip().split()]
    a = [int(x) for x in stdin.readline().rstrip().split()]
    aa = sorted(a)
    xx = x
    count = 0
    for i in aa:
        if i <= xx:
            xx -= i
            count += 1
        else:
            xx = 0
            break
    if xx != 0:
        count -= 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
ans = 0
judge = [False for i in range(N)]
True_X=x
# print(judge)
for i in range(N):
    if x-a[i]>=0:
        judge[i]=True
        x-=a[i]
temp_count=0
for i in range(N):
    if judge[i]:
        temp_count+=1
# print(judge)
if sum(a)<True_X:
    print(temp_count-1)
else:
    print(temp_count)

Submission status: Accepted

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