peterverebics/CodeNet_Python_118 huggingface dataset:
n, k = map(int, input().split())
h = [int(i) for i in input().split()]

dp = [1e5]*n
dp[0] = 0

for i in range(1, n):
    for j in range(max(0, i-k), i):
        dp[i] = min(dp[j]+abs(h[i]-h[j]), dp[i])

print(dp[n-1])
import numpy as np
import sys

n, k = map(int, input().split())
dp = np.full(n, 100005, dtype=int)
h = np.array([int(x) for x in input().split()])
dp[0] = 0
for i in range(n):
	j = 1
	while i - j >= 0 and j <= k:
		dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
		j += 1
print(dp[n - 1])
import numpy as np

n, k = map(int, input().split())
arr = np.concatenate(
    [np.array(list(map(int, input().split()))), np.zeros(k, dtype=int)])

INF = 1 << 20

# dp初期化
dp = np.full(n + k, INF, dtype=int)
dp[0] = 0

for i in range(n):
    for j in range(1, k + 1):
        dp[i + j] = min(dp[i + j], dp[i] + np.abs(arr[i + j] - arr[i]))

print(dp[n - 1])
n,k=map(int,input().split())
h=[int(x) for x in input().split()]
a=[10000]*(n+k+1)
b=[0]*(k)
h.extend(b)
a[0]=0
for i in range(n):
  for j in range(1,k+1):
    a[i+j]=min(a[i+j], a[i]+abs(h[i]-h[i+j]))
print(a[n-1])
import numpy as np
N, K = map(int, input().split())
#Hs = [int(i) for i in input().split()]
*Hs, = map(int, input().split())

DP_list = [np.inf for _ in range(N)]
DP_list[0] = 0

for i in range(1, N):
  for j in range( max(i-K,0), i):
    if j >= 0:
      this_time_score = DP_list[j] + np.abs(Hs[j] - Hs[i])
      DP_list[i] = min(DP_list[i] , this_time_score)

print(DP_list)
n, k = map(int, input().split())
l = list(map(int, input().split()))

dp = [float('inf') for _ in range(n)]
dp[0] = 0

for i in range(n-1):
    for j in range(1, k+1):
        if i+j <= n-1:
            temp = dp[i] + abs(l[i+j] - l[i])
            if dp[i] + temp < dp[i+j]:
                dp[i+j] = temp

print(dp[n-1])
N, K = map(int, input().split())
h = list(map(int, input().split()))

dp = [0] * N

for i in range(1, N):
    m = 10**8
    for j in range(1, K+1):
        k = i - j
        if k >= 0:
            m = min(m, abs(h[i] - h[k]) + dp[k])
    dp[i] = m
print(dp[N-1])
N,K=map(int,input().split())
listH=list(map(int,input().split()))
cost=[10**5]*N
cost[0]=0
for i in range(1,N):
  for k in range(1,K+1):
    if i-k>=0:
      cost[i]=min(abs(listH[i-k]-listH[i])+ cost[i-k],cost[i])
ans=cost[-1]      
print(ans)
N, K = map(int,input().split())
h = list(map(int,input().split()))

dp = [10000000]*(N)
dp[0]=0
for i in range(1,N):
  for j in range(0,K+1):
    if 0<=i-j:
      dp[i]=min(dp[i],dp[i-j]+abs(h[i-j]-h[i]))
 
print(dp[-1])
n, k = map(int, input().split())
h = list(map(int, input().split()))
MAXINT = 10**8
dp = [MAXINT for _ in range(n)]
dp[0] = 0

for i in range(n-1):
    for j in range(1, k+1):
        if i+j>=n:
            break
        if dp[i+j] > dp[i] + abs(h[i] - h[i+j]):
            dp[i+j] = dp[i] + abs(h[i] - h[i+j])

print(dp[n-1])
N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [float('inf')]*N
dp[0] = 0
for i in range(N-1):
    for j in range(1,min(K+1,N-i)):
        dp[i+j] = min(dp[i+j],dp[i]+abs(h[i]-h[i+j]))
print(dp[-1])
print(dp)
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [1000000000 for _ in range(n)]
for i in range(n):
  for j in range(1, k+1):
    if i + j >= n:
      break
    dp[i+j] = min(dp[i+j], dp[i] + abs(h[j]-h[i]))
ans = dp[n-1]
print(ans)
N,K=map(int,input().split())
listH=list(map(int,input().split()))
cost=[10**5]*N
cost[0]=0
for i in range(1,N):
  for k in range(min(i+1,K)):
    cost[i]=min(abs(listH[i-k-1]-listH[i])+ cost[i-k-1],cost[i])
ans=cost[-1]      
print(ans)
N,K=map(int,input().split())
A=list(map(int,input().split()))
A+=[0]*N
dp=[float("INF")]*(2*N)
dp[0]=0
for i in range(N-K+1):
  for j in range(1,K+1):
    dp[i+j]=min(abs(A[i+j]-A[i])+dp[i],dp[i+j])
print(dp[N-1])
import numpy as np

N, K = map(int, input().split(' '))
h = np.array(list(map(int, input().split(' '))))
dp = np.zeros(len(h), dtype=int)
for i in range(1, len(h)):
    start = max(0, i-K)
    print(start)
    print(dp[start:i] + np.abs(h[i] - h[start:i]))
    dp[i] = min(dp[start:i] + np.abs(h[i] - h[start:i]))
print(dp[-1])
def main():
    n,k= map(int,input().split())
    h = [int(i) for i in input().split()]
    inf = 10**7
    dp = [inf]*100005
    dp[0] = 0
    for i in range(1,n):
        for j in range(1,min(k,i)+1):
            dp[i] = min(dp[i],dp[i-j] + abs(h[i-j] - h[i]))
    print(dp[n-1])
main()
n,m = map(int,input().split())
l = list(map(int,input().split()))
dp = [0]+ [100000000]*(n-1)

for i in range(1,n):
    ans = dp[i]
    for j in range(1,m+1):
        if i-j >= 0:
            ans = min(ans,dp[i-j]+abs(l[i]-l[i-j]))
    dp[i] = ans
print(dp[n-1])
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,k=nm()
h=nl()

dp = [10**5] * n
dp[0] = 0

for i in range(n):
  for j in range(max(0,i-k),i):
    dp[i] = min(dp[i], dp[j] + abs(h[j]-h[i]))

print(dp[n-1])
n, k = list(map(int, input().split()))
nums = list(map(int, input().split()))

dp = [0 for _ in range(n)]

for i in range(1, n):
  for j in range(max(0, i-k), i):
    dp[i] = min(dp[i], dp[j] + abs(nums[i]-nums[j]))
print(dp[-1])
import sys
reader = (s.rstrip() for s in sys.stdin)
MAX = 100000000
input = reader.__next__
# do magic here
n, k = map(int, input().split())
heights = [int(x) for x in input().split()]

dp = [0 for x in range(n)]

for x in range(1, n):
    i = x-1
    ans = MAX
    while i >= max(0, x-k):
        ans = min(ans, abs(heights[x]-heights[i]) + dp[i])
        i -= 1
    dp[x] = ans
print(dp[n-1])
"""
author : halo2halo
date : 4, Feb, 2020
"""

import sys

# import itertools
# import math

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
INF = 10 ** 9

N, K, *H = map(int, read().split())
dp = [0] * N

for i in range(1, N):
    dp[i] = dp[i - 1] + abs(H[i] - H[i - 1])
    for j in range(1,K+1):
        if i >= j:
            dp[i] = min(dp[i],dp[i - j] + abs(H[i] - H[i - j]))
    print(dp)

print(dp[N-1])
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [10 ** 5] * n
dp[0] = 0
for i in range(0, n):
    for j in range(1, k + 1):
        if j > n:
            break
        dp[i] = min(dp[i], dp[i-j] + abs(h[i] - h[i - j]))
print(dp[n - 1])
N, K = map(int,input().split())
h = list(map(int,input().split()))

dp = [0] * N
dp[1] = abs(h[0] - h[1])

for i in range(2, N):
    dp[i] = min([dp[j] + abs(h[i] - h[j]) for j in range(i - K, i) if j >= 0])

print(dp)
n,k= map(int,input().split())
h1=list(map(int,input().split()))
if k>=n-1:
    print(abs(h1[0]-h1[n-1]))
else:
    dp=[-1]*n
    dp[n-1]=0
    for y in range(n-k-1,n-1):
       dp[y]=abs(h1[n-1]-h1[y])    
    for u in range(k+1,n):
     i=(n-1-u)
     dp[i]=min([dp[i+h]+abs(h1[i+h]-h1[i]) for h in range(1,k+1)])
    print(dp[0],dp)
N,K=map(int,input().split())
H=list(map(int,input().split()))
dp = [10**6]*N
dp[0]=0
for i in range(N-1):
    for j in range(1,min(K+1,N-i)):
        dp[i+j]=min(dp[i+j],dp[i]+abs(H[i+j]-H[i]))
print(dp[-1])
x,k=map(int,input().split())
a=list(map(int,input().split()))
dp=[0]*x
dp[1]=abs(a[1]-a[0])
for i in range(2,x):
    p=100000001
    #print(i-k,i)
    for j in range(max(0,i-k),i):
        #print(i,j)
        p=min(p,dp[j]+abs(a[i]-a[j]))
        
    dp[i]=p
print(dp[x-1])
[N,K]=list(map(int, input().split()))
h=list(map(int, input().split()))
K=min(N,K)
 
dp=[[0 for i in range(K+1)] for j in range(N)]
 
for i in range (1,K):
    for j in range (0,i):
        dp[i][j]=dp[i-j-1][K]+abs(h[i-j-1]-h[i])
        dp[i][K]=min(dp[i][0:j+1])
 
for i in range (K,N):
    for j in range (0,K):
        dp[i][K]=min(dp[i][0:K])
 
print(dp[N-1][K])
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10**5

DP = [INF] * ((10**5)+10)
DP[0] = 0

for i in range(N):
    for j in range(1, K+1):
        if i + j < N:
            DP[i+j] = min(DP[i+j], DP[i] + abs(H[i+j] - H[i]))

# print(DP)
print(DP[N-1])
import math
N, K = map(int, input().split())
h = list(map(int, input().split()))

S = [0] * N
S[0] = 0

for i in range(1, N):
    value = abs(h[i] - h[i - 1])
    for step in range(1, K + 1):
        # print("i, k:", i, k)
        if i - step < 0:
            break
        value = min(value, S[i - step] + abs(h[i] - h[i - step]))
        #print("value", value)
    S[i] = value
    # print(S)
# print(S)
print(S[N - 1])
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
n,k=mi()
h=li()
INF=10*9
dp = [0]*n
for i in range(1,n):
    st = max(0,i-k)
    comp = INF
    for j in range(st,i):
        comp = min(comp,dp[j]+abs(h[i] - h[j]))
    dp[i] = comp
print(dp[n-1])
N, K = map(int, input().split())
h = [int(x) for x in input().split()]


# list dp retains information for the minimum costs
# if dp[i] = n, it means n is the minimum cost to get to i step
# if dp[i] = INF, it means the minimum cost has not been evaluated yet
INF = float('inf')
dp = [INF for _ in range(N+K+10)]

# initial state
dp[0] = 0

for i in range(N):
    for j in range(1, min(K+1, len(h)-i)):
        dp[i+j] = min(dp[i] + abs(h[j+i] - h[i]), dp[i+j])
        
dp[N-1]
N, K = map(int, input().split())
H = list(map(int, input().split()))
INF = 10**4

dp = [INF]*N
dp[0] = 0

for i in range(N-1):
  for j in range(1, min(K+1, N-i)):
    dp[i+j] = min(dp[i+j], dp[i] + abs(H[i+j] - H[i]))

print(dp[-1])
size, jumps = map(int, input().split())
array = list(map(int, input().split()))
dp = [9999999] * size
dp[0]= 0
for i in range(size):
    for j in range(i + 1, i+jumps+1):
        if j < size:
            dp[j] = min(dp[j], dp[i]+abs(array[i] - array[j]))
print(dp[size-1])
N, K = map(int, input().split())
[*h] = map(int, input().split())
dp = [10**4 for _ in range(N)]
dp[0] = 0
for i in range(N):
  for j in range(1, min(i, K)+1):
    dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))
 
print(dp[N-1])
N, K = map(int, input().split())
h = [int(i) for i in input().split()]
dp = [10**6] * N
dp[0] = 0
dp[1] = abs(h[1]-h[0])
for i in range(1, N):
    for j in range(1, min(K,i)+1):
        dp[i] = min(dp[i], dp[i-j] + abs(h[i-j] - h[i]))

print(dp[N-1])
import math
def min_cost(arr):
    n = len(arr)
    dp = [100000000 for _ in range(n)]
    dp[0] = 0
    dp[1] = abs(arr[1]- arr[0])
    for i in range(2,n):
        for j in range(1,k+1):
            if i - j >= 0:
                #print(i)
                dp[i] =  min(dp[i],dp[i-j] + abs(arr[i]-arr[i-j]))
        
    return dp[n-1]

n,k = map(int,input().split())
arr = list(map(int,input().split()))
print(min_cost(arr))
##Educational-DP B
n,k = map(int,input().split())
h = list(map(int,input().split()))

dp = [999999] * n
dp[0] = 0

for i in range(1,n):
    for j in range(max(0,i-k),i):
        dp[i] = min(dp[i],dp[j]+abs(h[j]-h[i]))
        
#print(dp)
print(dp[-1])
N,K= map(int,input().split())
H = list(map(int,input().split()))
dp = [0]*N

dp[1] = abs(H[1]-H[0])
for i in range(2,N):
  minimum = 10**9
  for j in range(1,K+1):
    if i-j<0:
      break
    else:
      if dp[i-j]-abs(H[i]-H[i-j]) < minimum:
        minimum = dp[i-j]+abs(H[i]-H[i-j])
  dp[i] = minimum
print(dp[-1])
N,K=map(int,input().split())
listH=list(map(int,input().split()))
cost=[10**5]*N
cost[0]=0
for i in range(1,N):
  for k in range(K+1):
    if i-k>=0:
      cost[i]=min(abs(listH[i-k]-listH[i])+ cost[i-k],cost[i])
ans=cost[-1]      
print(ans)
[N,K]=[int(i) for i in input().split()]
h=[int(i) for i in input().split()]
DP=[140000 for i in range(N)]
DP[0]=0
for i in range(N):
	for j in range(max(i-K,0),i):
		DP[i]=min(DP[j]+abs(h[i]-h[j]),DP[i])


print(DP[N-1])
N, K = map(int, input().split())
arr = list(map(int, input().split()))
dp = [10**6] * N
dp[0] = 0
for i in range(0,N):
    k = 1
    while (i + k < N) and (k <= K):
        dp[i+k] = min(abs(arr[i]-arr[i+k])+dp[i],dp[i+k])
        k += 1
print(dp[N-1])
N,K=map(int,input().split())
H=list(map(int,input().split()))
DP=[10**8]*N
DP[0]=0

for i in range(1,N):
    for j in range(max(0,i-K),i):
        DP[i]=min(DP[i],abs(H[i]-H[j])+DP[j])

print(DP[-1])
N, K = map(int, input().split())
H = list(map(int, input().split()))

INF = 10**4 + 1
L = [INF]*N
L[0] = 0
for i in range(1, N):
    for k in range(1, K+1):
        if i - k >= 0:
            L[i] = min(L[i], L[i-k] + abs(H[i] - H[i-k]))
print(L[N-1])
N, K = map(int, input().split())
h = list(map(int, input().split()))
t = pow(10,10)
dp_h = [t] * N
dp_h[1] = abs(h[1] - h[0])
for i in range(2,N):
  tmp_min = dp_h[i]
  for j in range(min(i, K)):
    tmp = abs(h[i] - h[i - j - 2]) + dp_h[i-j-2]
    if h[i] - h[i-j-2] == 0:
      tmp_min = tmp
      break
    if tmp < tmp_min:
      tmp_min = tmp
  dp_h[i] = tmp_min
print(dp_h[N-1])
N,K = map(int,input().split())
h = list(map(int,input().split()))
#print(h)
dp = [9999 for i in range(N)]
dp[0] = 0
#print(dp)
for i in range(N):
  for j in range(K+1):
    if N-1 < i+j:
      break
    else:
      dp[i+j]  = min(dp[i+j],dp[i] + abs(h[i] - h[i+j]))
print(dp[N-1])
def function(l,n,k):
    dp=[1e6]*(n+1)
    dp[0]=0
    for i in range(n):
        for j in range(1,k+1):
            if i+j<=n:
                dp[i+j]=min(dp[i]+abs(l[i]-l[i+j]),dp[i+j])
            else:
                break
    print(dp[n])
n,k=[int(i) for i in input().split()]
l=[int(i) for i in input().split()]
function(l,n-1,k)
N, x = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
ans = 0
for a in A[:-1]:
    if a <= x:
        ans += 1
        x -= a
print(ans + 1 if A[-1] == x else ans)
N,x=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
for i in range(len(A)):
  if x-A[i]<0:
    print(i)
    exit()
  else:
    x-=A[i]
if x==0:print(N)
else:print(N-1)
N, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
count = 0
for i in range(len(a)):
    if x - a[i] >= 0:
        x -= a[i]
        count += 1
    else:
        break
if count == N and x  > 0:
    print(count - 1)
else:
    print(count)
N,x = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

cnt = 0
sum_x = 0
for i in range(N):
  if(i == N-1):
    if((x-sum_x) == A[N-1]):
      cnt += 1
    break
  
  if(sum_x+A[i] <= x):
    sum_x += A[i]
    cnt += 1
  else:
    break
   
    
print(cnt)
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np


def main(n, x, a):
  cnt = 0
  for j, i in enumerate(a):
    x -= i
    if x < 0:
      break
    elif j == n-1 and x > 0:
      break
    cnt += 1
  return cnt

n, x = map(int, readline().split())
a = np.sort(np.array(readline().split(), np.int64))
print(main(n, x, a))
N,x = map(int,input().split())
a = list(map(int, input().split()))
a.sort()
cnt=0

for i in a:
    x-=i
    if x>=0:
        cnt+=1

if x>0:
    cnt-=1

print(cnt)
n, x = map(int, input().split())
A = sorted([int(i) for i in input().split()])
# print(A)
cnt = 0
for e in A:
    if e <= x:
        x = x - e
        cnt += 1
if cnt == n and x > 0:
    print(cnt-1)
else:
    print(cnt)
n,x = map(int,input().split())
a = list(map(int,input().split()))
a = sorted(a)
count = 0
s = 0
for i in range(n):
    if i == n-1:
        if x == a[i]:
            count += 1
    else:
        s += a[i]
        if x > a[i]:
            x -= a[i]
            count += 1
        elif x < a[i]:
            break
        else:
            count += 1
            break
print(count)
N, x = map(int, input().split())
a = list(map(int, input().split()))
p = x
a.sort()
count = 0

for i in a:
  if x - i >= 0:
    x -= i
    count += 1
if x > 0 and count != 0  and p > sum(a):
  count -= 1
print(count)
N, x = map(int,input().split())
a = list(map(int,input().split()))
count = 0
a_new = sorted(a)

for i in range(0, N):
	count += a_new[i]
	if x == count:
		index = i + 1
		break
	if count > x:
		index = i
		break
	index = i

print(str(index))
n,x=map(int,input().split())
a=sorted(list(map(int,input().split())))
for i in range(n):
    x-=a[i]
    if x<0:
        break
    else:
        if x==0:
            i+=1
print(i)
N, x = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

if sum(A) < x:
    print(N-1)
    exit()

A = [1] + A
ans = 0
while A[-1] <= x:
    x -= A[-1]
    A.pop()
    ans += 1
print(ans)
N,x = map(int,input().split())
a = sorted(list(map(int,input().split())))
s = [sum(a[:i+1]) for i in range(N)]

if s[-1] == x:
  print(N)
  exit()
else:
  for i in range(N-1):
    if s[i] > x:
      print(i)
      exit()
      
  print(N-1)
N,x = map(int,input().split())
A = sorted(list(map(int,input().split())))
ans = 0
for a in A:
    x = x-a
    if x<0:
        break
    ans+=1
print(ans-(x>0))
import sys
input = lambda :sys.stdin.readline().rstrip()

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

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

print(cnt)
n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 0
if x == sum(a):
    ans = n
elif x > sum(a):
    ans = n-1
else:
    for i in range(n):
        if x - a[i] >= 0:
            x -= a[i]
            ans += 1
        else:
            break
print(ans)
def resolve():
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 0
    a = sorted(a)
    for i in range(n):
        if x < 0:
            break
        x -= a[i]
        ans += 1
    if x != 0:
        ans -= 1
    print(ans)
resolve()
N, x = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

ans = 0
for a in A[:-1]:
    if x - a >= 0:
        x -= a
        ans += 1
print(ans + (A[-1] == x))
n,x=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*n
a.sort()
cnt=0
for i in range(n):
    if a[i]<=x:
        x-=a[i]
        if i==n-1 and x!=0:
            break
        cnt+=1
    else:
        break
print(cnt)
N, X = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
res = 0
for a in A:
    if a > X:
        break
    res += 1
    X -= a
else:
    if X > 0:
        res -= 1
print(max(res, 0))
N,x = map(int, input().split())
a = list(map(int, input().split()))

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

if x == 0 or ans < N:
  print(ans)
else:
  print(ans-1)
N,x=map(int,input().split())
a=list(map(int,input().split()))
a_sort=sorted(a,reverse=False)
tot=0
if sum(a)==x:
    print(len(a))
elif sum(a)<x:
    print(len(a)-1)
else:
    for i in range(N):
        tot+=a_sort[i]
        if tot==x:
            print(i+1)
            break
        elif tot<x:
            pass
        elif tot>x:
            print(i)
            break
n, x = map(int, input().split())
a = list(map(int, input().split()))

sort_a = sorted(a)
count = 0
if x > sum(a):
  count-=1
for i in sort_a:
  if x >= i:
    count+=1
    x-=i
  else:
    print(count)
    exit()
print(count)
# A - Candy Distribution Again

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

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

print(ans)
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    n, x = map(int, input().split())
    aaa = list(map(int, input().split()))
    aaa.sort()
    ans = 0
    for a in aaa:
        if x < a:
            break
        ans += 1
        x -= a
    else:
        if x > 0:
            ans -= 1
    print(ans)


if __name__ == '__main__':
    main()
from bisect import bisect_right
from itertools import accumulate

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

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


happy = bisect_right(arr, x)

if happy == n:
    if arr[-1] != x:
        happy -= 1

print(happy)
N, x = map(int, input().split())
a = sorted(map(int, input().split()))
cnt = 0
for i in a:
    if x < i:
        break
    cnt += 1
    x -= i
else:
    if x > 0:
        cnt -= 1
print(cnt)
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, x = map(int, input().split())
    A = sorted(list(map(int, input().split())))

    res = 0
    for i in range(n):
        if i == n - 1 and A[i] < x:
            continue

        if A[i] <= x:
            x -= A[i]
            res += 1

    print(res)


if __name__ == '__main__':
    resolve()
#!/usr/bin/env python
# coding: utf-8

# In[17]:


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


# In[18]:


a_sum = sum(a)
if a_sum == x:
    print(N)
elif a_sum < x:
    print(N-1)
else:
    for i in range(N):
        if x < sum(sorted(a, reverse=False)[:i+1]):
            print(i)
            break


# In[ ]:
n,x=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
num=0
for i in range(n):
  num+=a[i]
  if num>x:
    print(i)
    exit()
if num==x:
  print(n)
else:
  print(n-1)
N, x = map(int, input().split())
A_list = list(map(int, input().split()))
A_list = sorted(A_list)
for i in range(N):
    x -= A_list[i]
    if x <= 0:
        break
print(i+1) if x == 0 else print(i)
N, x = map(int, input().split())
A = [int(i) for i in input().split()]

A.sort()
ans = []
for i in A:
	x -= i
	#print(ans, x)
	if x >= 0:
		ans.append(i)
	else:
		break
	
if x > 0:
	print(len(ans)-1)
else:
	print(len(ans))
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, x, *A = map(int, read().split())
A = sorted(A)
cnt = 0

for a in A[:-1]:
    if x >= a:
        cnt += 1
        x -= a
if x == A[-1]:
    cnt += 1

print(cnt)
n,x=map(int,input().split())
a=list(map(int,input().split()))
ans=0

a.sort()

for i in a:
  x-=i
  if x>0:
    ans+=1
    continue
  
  elif x==0:
    ans+=1
    break
    
  else:
    break
    
if ans==n:
  if x!=0:
    ans-=1


print(ans)
N, x = map(int, input().split())
array = list(map(int, input().split()))
a = sorted(array, reverse=False)
#print(a)
answer = 0
for i in range(N):
    x -= a[i]
    if x < 0:
        break
    answer += 1
    if i == N-1 and x != 0:
        answer -= 1
        break
    if x < a[i]:
        break
print(answer)
n,x = map(int,input().split())
al = list(map(int,input().split()))
al.sort()
c = 0
for a in al:
  if x >= a:
    c += 1
  x -= a
if x > 0 and c > 0:
  c -= 1
print(c)
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n, x = inintm()
A = inintl()

A.sort()
ans = 0

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

if A[-1] == x:
    ans += 1

print(ans)
N,x = map(int,input().split())
A = sorted(list(map(int,input().split())))
if x>=2*sum(A):
    print(0)
else:
    ind = N
    for i in range(N):
        x -= A[i]
        if x<0:
            ind = i
            break
    if ind==N and x>0:
        A = sorted(A,reverse=True)
        for i in range(N):
            x -= A[i]
            if x<0:
                ind -= 1
                break
    print(ind)
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
cnt = 0
for i in range(n):
	k -= a[i]
	if k >= 0:
		cnt += 1
	else:
		break
if k > 0:
	cnt -= 1

print(cnt)
n,x=map(int,input().split())
l=list(map(int,input().split()))
l=sorted(l)
ans=0
for i in l:
    x-=i
    ans+=1
    if x<0:
        break
if x!=0 and ans>0:    
    print(ans-1)
else:
    print(ans)
n, a = map(int, input().split())
b = list(map(int, input().split()))
b.sort()
c = 0
for i in range(n):
  if a < b[i] or (i == n - 1 and b[i] != a):
    break
  a -= b[i]
  c += 1
print(c)
n, x = list(map(int, input().split()))
lista = list(map(int, input().split()))

lista.sort()

snack = 0

for i in range(n):
  if lista[i] + snack <= x:
    snack += lista[i]
  else:
    print(i)
    break
else:
  if snack == x:
    print(n)
  else:
    print(n - 1)