Alyona has recently bought a miniature fridge that can be represented as a matrix with $h$ rows and $2$ columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part.

 [Image] An example of a fridge with $h = 7$ and two shelves. The shelves are shown in black. The picture corresponds to the first example. 

Alyona has $n$ bottles of milk that she wants to put in the fridge. The $i$-th bottle is $a_i$ cells tall and $1$ cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.

Alyona is interested in the largest integer $k$ such that she can put bottles $1$, $2$, ..., $k$ in the fridge at the same time. Find this largest $k$.


-----Input-----

The first line contains two integers $n$ and $h$ ($1 \le n \le 10^3$, $1 \le h \le 10^9$) — the number of bottles and the height of the fridge.

The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le h$) — the heights of the bottles.


-----Output-----

Print the single integer $k$ — the maximum integer such that Alyona can put the bottles $1$, $2$, ..., $k$ in the fridge at the same time. If Alyona can put all bottles in the fridge, print $n$. It is easy to see that Alyona can always put at least one bottle in the fridge.


-----Examples-----
Input
5 7
2 3 5 4 1

Output
3

Input
10 10
9 1 1 1 1 1 1 1 1 1

Output
4

Input
5 10
3 1 4 2 4

Output
5



-----Note-----

One of optimal locations in the first example is shown on the picture in the statement.

One of optimal locations in the second example is shown on the picture below.

 [Image] 

One of optimal locations in the third example is shown on the picture below.

 [Image]

Difficulty: EASY
Tags: Sorting

Flows and cuts

Greedy algorithms
Skill types: Sorting

Greedy algorithms

Starter code:

Solutions:
(n, h) = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
ar = []
ans = 0
for i in range(len(arr)):
	ar.append(arr[i])
	ar = sorted(ar)
	val = 0
	j = len(ar) - 1
	while j >= 0:
		if j != 0:
			val += max(ar[j], ar[j - 1])
		else:
			val += ar[j]
		j -= 2
	if val <= h:
		ans = max(ans, i + 1)
print(ans)


import sys, math, queue
sys.setrecursionlimit(1000000)

def canPut(k, height, bot):
	bot.sort(reverse=True)
	for i in range(k):
		if bot[i] > height:
			return False
		if i % 2 == 1:
			height -= bot[i - 1]
	return True
(n, height) = map(int, input().split())
bot = list(map(int, input().split()))
(l, h) = (1, n)
while l <= h:
	m = (l + h) // 2
	if canPut(m, height, bot[:m]):
		ans = m
		l = m + 1
	else:
		h = m - 1
print(ans)


def main():
	(n, h) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	ans = 0
	for i in range(len(a)):
		b = sorted(a[0:i + 1])
		sumh = 0
		j = len(b) - 1
		while j >= 0:
			sumh += b[j]
			j -= 2
		if sumh <= h:
			ans = len(b)
	print(ans)
main()


def heightneeded(l, n):
	total = 0
	i = n + 1
	while i - 2 >= 0:
		i -= 2
		total += l[i]
	return total
(n, h) = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
k = 0
while k < n and heightneeded(sorted(l[:k + 1]), k + 1) <= h:
	k += 1
print(k)


(n, h) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
cur = []
Max = 0
for k in range(1, n + 1):
	for i in range(k):
		cur.append(a[i])
	cur = sorted(cur)
	cur.reverse()
	summ = 0
	for i in range(0, k, 2):
		summ += cur[i]
	if summ <= h:
		Max = k
	cur.clear()
print(Max)


import bisect
a = []
(n, h) = list(map(int, input().split()))
k = 0
for ai in input().split():
	bisect.insort(a, int(ai))
	if sum(a[::-2]) > h:
		break
	k += 1
print(k)


(n, h) = map(int, input().split())
arr = [int(j) for j in input().split()]
s = 0
e = n - 1
ans = -1
while s <= e:
	mid = (s + e) // 2
	temp = arr[:mid + 1]
	temp.sort(reverse=True)
	if len(temp) % 2 == 1:
		temp += [0]
	cur = 0
	ind = 0
	while ind < len(temp) and cur + max(temp[ind], temp[ind + 1]) <= h:
		cur += max(temp[ind], temp[ind + 1])
		ind += 2
	if ind == len(temp):
		ans = mid
		s = mid + 1
	else:
		e = mid - 1
print(ans + 1)


from bisect import bisect_left
(n, h) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))

def canPack(arr, h):
	milk = sorted(arr, reverse=True)
	(left, right) = (h, h)
	for (i, m) in enumerate(milk):
		if i % 2 == 0:
			if m > left:
				return False
			left -= m
		if i % 2 == 1:
			if m > right:
				return False
			right -= m
			left = min([left, right])
			right = left
	return True

def binary_search(arr, low=0, high=None):
	if high is None:
		high = len(arr)
	while low < high:
		mid = (low + high) // 2
		midval = arr[mid]
		if canPack(arr[:mid], h):
			low = mid + 1
		else:
			high = mid
	return low
if canPack(a, h):
	print(n)
else:
	print(binary_search(a) - 1)


import sys
input = sys.stdin.readline
(n, origH) = map(int, input().split())
b = list(map(int, input().split()))
output = ''
for k in range(1, n + 1):
	fits = 0
	h = origH
	bottles = sorted(b[:k], reverse=True)
	valid = True
	for i in range(0, k - 1, 2):
		if bottles[i] <= h:
			fits += 2
			h -= bottles[i]
		else:
			valid = False
			break
	if k % 2 and bottles[-1] > h:
		valid = False
	if valid:
		output = k
print(output)


def cout(mid, lst):
	res = list(lst[:mid])
	res.sort()
	result = 0
	for i in range(mid - 1, -1, -2):
		result += res[i]
	return result
(n, h) = map(int, input().split())
lst = [*map(int, input().split())]
(i, j) = (1, n)
while i + 1 < j:
	mid = (i + j) // 2
	result = cout(mid, lst)
	if result > h:
		j = mid
	else:
		i = mid
if cout(j, lst) <= h:
	print(j)
else:
	print(i)


(n, h) = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
for i in range(n):
	curr = 0
	ls = sorted(l[:i + 1])
	for j in range(i, -1, -2):
		curr += ls[j]
	if curr > h:
		break
	else:
		ans = i
print(ans + 1)


import heapq
(n, h) = map(int, input().split())
a = list(map(int, input().split()))
temp_a = []
heapq.heapify(temp_a)
k = 0
tmp_h = h
while tmp_h >= 0:
	if k < n:
		heapq.heappush(temp_a, a[k])
		k += 1
		largest = heapq.nlargest(k, temp_a)
		tmp_h = h
		for i in range(0, k, 2):
			tmp_h -= largest[i]
	else:
		print(n)
		break
else:
	print(k - 1)


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

def isok(k):
	if k == 0:
		return True
	sa = list(sorted(A[:k], reverse=True))
	h = 0
	for a in sa[::2]:
		h += a
		if h > H:
			return False
	return True
ok = 0
ng = N + 1
while ng - ok > 1:
	m = (ok + ng) // 2
	if isok(m):
		ok = m
	else:
		ng = m
print(ok)


from __future__ import print_function
from bisect import bisect_left, bisect_right, insort
from collections import Sequence, MutableSequence
from itertools import chain, repeat, starmap
from math import log
from operator import add, eq, ne, gt, ge, lt, le, iadd
from textwrap import dedent
from functools import wraps
from sys import hexversion
if hexversion < 50331648:
	from itertools import imap as map
	from itertools import izip as zip
	try:
		from thread import get_ident
	except ImportError:
		from dummy_thread import get_ident
else:
	from functools import reduce
	try:
		from _thread import get_ident
	except ImportError:
		from _dummy_thread import get_ident

def recursive_repr(fillvalue='...'):

	def decorating_function(user_function):
		repr_running = set()

		@wraps(user_function)
		def wrapper(self):
			key = (id(self), get_ident())
			if key in repr_running:
				return fillvalue
			repr_running.add(key)
			try:
				result = user_function(self)
			finally:
				repr_running.discard(key)
			return result
		return wrapper
	return decorating_function

class SortedList(MutableSequence):
	DEFAULT_LOAD_FACTOR = 1000

	def __init__(self, iterable=None, key=None):
		assert key is None
		self._len = 0
		self._load = self.DEFAULT_LOAD_FACTOR
		self._lists = []
		self._maxes = []
		self._index = []
		self._offset = 0
		if iterable is not None:
			self._update(iterable)

	def __new__(cls, iterable=None, key=None):
		if key is None:
			return object.__new__(cls)
		elif cls is SortedList:
			return object.__new__(SortedKeyList)
		else:
			raise TypeError('inherit SortedKeyList for key argument')

	@property
	def key(self):
		return None

	def _reset(self, load):
		values = reduce(iadd, self._lists, [])
		self._clear()
		self._load = load
		self._update(values)

	def clear(self):
		self._len = 0
		del self._lists[:]
		del self._maxes[:]
		del self._index[:]
		self._offset = 0
	_clear = clear

	def add(self, value):
		_lists = self._lists
		_maxes = self._maxes
		if _maxes:
			pos = bisect_right(_maxes, value)
			if pos == len(_maxes):
				pos -= 1
				_lists[pos].append(value)
				_maxes[pos] = value
			else:
				insort(_lists[pos], value)
			self._expand(pos)
		else:
			_lists.append([value])
			_maxes.append(value)
		self._len += 1

	def _expand(self, pos):
		_load = self._load
		_lists = self._lists
		_index = self._index
		if len(_lists[pos]) > _load << 1:
			_maxes = self._maxes
			_lists_pos = _lists[pos]
			half = _lists_pos[_load:]
			del _lists_pos[_load:]
			_maxes[pos] = _lists_pos[-1]
			_lists.insert(pos + 1, half)
			_maxes.insert(pos + 1, half[-1])
			del _index[:]
		elif _index:
			child = self._offset + pos
			while child:
				_index[child] += 1
				child = child - 1 >> 1
			_index[0] += 1

	def update(self, iterable):
		_lists = self._lists
		_maxes = self._maxes
		values = sorted(iterable)
		if _maxes:
			if len(values) * 4 >= self._len:
				values.extend(chain.from_iterable(_lists))
				values.sort()
				self._clear()
			else:
				_add = self.add
				for val in values:
					_add(val)
				return
		_load = self._load
		_lists.extend((values[pos:pos + _load] for pos in range(0, len(values), _load)))
		_maxes.extend((sublist[-1] for sublist in _lists))
		self._len = len(values)
		del self._index[:]
	_update = update

	def __contains__(self, value):
		_maxes = self._maxes
		if not _maxes:
			return False
		pos = bisect_left(_maxes, value)
		if pos == len(_maxes):
			return False
		_lists = self._lists
		idx = bisect_left(_lists[pos], value)
		return _lists[pos][idx] == value

	def discard(self, value):
		_maxes = self._maxes
		if not _maxes:
			return
		pos = bisect_left(_maxes, value)
		if pos == len(_maxes):
			return
		_lists = self._lists
		idx = bisect_left(_lists[pos], value)
		if _lists[pos][idx] == value:
			self._delete(pos, idx)

	def remove(self, value):
		_maxes = self._maxes
		if not _maxes:
			raise ValueError('{0!r} not in list'.format(value))
		pos = bisect_left(_maxes, value)
		if pos == len(_maxes):
			raise ValueError('{0!r} not in list'.format(value))
		_lists = self._lists
		idx = bisect_left(_lists[pos], value)
		if _lists[pos][idx] == value:
			self._delete(pos, idx)
		else:
			raise ValueError('{0!r} not in list'.format(value))

	def _delete(self, pos, idx):
		_lists = self._lists
		_maxes = self._maxes
		_index = self._index
		_lists_pos = _lists[pos]
		del _lists_pos[idx]
		self._len -= 1
		len_lists_pos = len(_lists_pos)
		if len_lists_pos > self._load >> 1:
			_maxes[pos] = _lists_pos[-1]
			if _index:
				child = self._offset + pos
				while child > 0:
					_index[child] -= 1
					child = child - 1 >> 1
				_index[0] -= 1
		elif len(_lists) > 1:
			if not pos:
				pos += 1
			prev = pos - 1
			_lists[prev].extend(_lists[pos])
			_maxes[prev] = _lists[prev][-1]
			del _lists[pos]
			del _maxes[pos]
			del _index[:]
			self._expand(prev)
		elif len_lists_pos:
			_maxes[pos] = _lists_pos[-1]
		else:
			del _lists[pos]
			del _maxes[pos]
			del _index[:]

	def _loc(self, pos, idx):
		if not pos:
			return idx
		_index = self._index
		if not _index:
			self._build_index()
		total = 0
		pos += self._offset
		while pos:
			if not pos & 1:
				total += _index[pos - 1]
			pos = pos - 1 >> 1
		return total + idx

	def _pos(self, idx):
		if idx < 0:
			last_len = len(self._lists[-1])
			if -idx <= last_len:
				return (len(self._lists) - 1, last_len + idx)
			idx += self._len
			if idx < 0:
				raise IndexError('list index out of range')
		elif idx >= self._len:
			raise IndexError('list index out of range')
		if idx < len(self._lists[0]):
			return (0, idx)
		_index = self._index
		if not _index:
			self._build_index()
		pos = 0
		child = 1
		len_index = len(_index)
		while child < len_index:
			index_child = _index[child]
			if idx < index_child:
				pos = child
			else:
				idx -= index_child
				pos = child + 1
			child = (pos << 1) + 1
		return (pos - self._offset, idx)

	def _build_index(self):
		row0 = list(map(len, self._lists))
		if len(row0) == 1:
			self._index[:] = row0
			self._offset = 0
			return
		head = iter(row0)
		tail = iter(head)
		row1 = list(starmap(add, zip(head, tail)))
		if len(row0) & 1:
			row1.append(row0[-1])
		if len(row1) == 1:
			self._index[:] = row1 + row0
			self._offset = 1
			return
		size = 2 ** (int(log(len(row1) - 1, 2)) + 1)
		row1.extend(repeat(0, size - len(row1)))
		tree = [row0, row1]
		while len(tree[-1]) > 1:
			head = iter(tree[-1])
			tail = iter(head)
			row = list(starmap(add, zip(head, tail)))
			tree.append(row)
		reduce(iadd, reversed(tree), self._index)
		self._offset = size * 2 - 1

	def __delitem__(self, index):
		if isinstance(index, slice):
			(start, stop, step) = index.indices(self._len)
			if step == 1 and start < stop:
				if start == 0 and stop == self._len:
					return self._clear()
				elif self._len <= 8 * (stop - start):
					values = self._getitem(slice(None, start))
					if stop < self._len:
						values += self._getitem(slice(stop, None))
					self._clear()
					return self._update(values)
			indices = range(start, stop, step)
			if step > 0:
				indices = reversed(indices)
			(_pos, _delete) = (self._pos, self._delete)
			for index in indices:
				(pos, idx) = _pos(index)
				_delete(pos, idx)
		else:
			(pos, idx) = self._pos(index)
			self._delete(pos, idx)

	def __getitem__(self, index):
		_lists = self._lists
		if isinstance(index, slice):
			(start, stop, step) = index.indices(self._len)
			if step == 1 and start < stop:
				if start == 0 and stop == self._len:
					return reduce(iadd, self._lists, [])
				(start_pos, start_idx) = self._pos(start)
				if stop == self._len:
					stop_pos = len(_lists) - 1
					stop_idx = len(_lists[stop_pos])
				else:
					(stop_pos, stop_idx) = self._pos(stop)
				if start_pos == stop_pos:
					return _lists[start_pos][start_idx:stop_idx]
				prefix = _lists[start_pos][start_idx:]
				middle = _lists[start_pos + 1:stop_pos]
				result = reduce(iadd, middle, prefix)
				result += _lists[stop_pos][:stop_idx]
				return result
			if step == -1 and start > stop:
				result = self._getitem(slice(stop + 1, start + 1))
				result.reverse()
				return result
			indices = range(start, stop, step)
			return list((self._getitem(index) for index in indices))
		else:
			if self._len:
				if index == 0:
					return _lists[0][0]
				elif index == -1:
					return _lists[-1][-1]
			else:
				raise IndexError('list index out of range')
			if 0 <= index < len(_lists[0]):
				return _lists[0][index]
			len_last = len(_lists[-1])
			if -len_last < index < 0:
				return _lists[-1][len_last + index]
			(pos, idx) = self._pos(index)
			return _lists[pos][idx]
	_getitem = __getitem__

	def __setitem__(self, index, value):
		message = 'use ``del sl[index]`` and ``sl.add(value)`` instead'
		raise NotImplementedError(message)

	def __iter__(self):
		return chain.from_iterable(self._lists)

	def __reversed__(self):
		return chain.from_iterable(map(reversed, reversed(self._lists)))

	def reverse(self):
		raise NotImplementedError('use ``reversed(sl)`` instead')

	def islice(self, start=None, stop=None, reverse=False):
		_len = self._len
		if not _len:
			return iter(())
		(start, stop, _) = slice(start, stop).indices(self._len)
		if start >= stop:
			return iter(())
		_pos = self._pos
		(min_pos, min_idx) = _pos(start)
		if stop == _len:
			max_pos = len(self._lists) - 1
			max_idx = len(self._lists[-1])
		else:
			(max_pos, max_idx) = _pos(stop)
		return self._islice(min_pos, min_idx, max_pos, max_idx, reverse)

	def _islice(self, min_pos, min_idx, max_pos, max_idx, reverse):
		_lists = self._lists
		if min_pos > max_pos:
			return iter(())
		if min_pos == max_pos:
			if reverse:
				indices = reversed(range(min_idx, max_idx))
				return map(_lists[min_pos].__getitem__, indices)
			indices = range(min_idx, max_idx)
			return map(_lists[min_pos].__getitem__, indices)
		next_pos = min_pos + 1
		if next_pos == max_pos:
			if reverse:
				min_indices = range(min_idx, len(_lists[min_pos]))
				max_indices = range(max_idx)
				return chain(map(_lists[max_pos].__getitem__, reversed(max_indices)), map(_lists[min_pos].__getitem__, reversed(min_indices)))
			min_indices = range(min_idx, len(_lists[min_pos]))
			max_indices = range(max_idx)
			return chain(map(_lists[min_pos].__getitem__, min_indices), map(_lists[max_pos].__getitem__, max_indices))
		if reverse:
			min_indices = range(min_idx, len(_lists[min_pos]))
			sublist_indices = range(next_pos, max_pos)
			sublists = map(_lists.__getitem__, reversed(sublist_indices))
			max_indices = range(max_idx)
			return chain(map(_lists[max_pos].__getitem__, reversed(max_indices)), chain.from_iterable(map(reversed, sublists)), map(_lists[min_pos].__getitem__, reversed(min_indices)))
		min_indices = range(min_idx, len(_lists[min_pos]))
		sublist_indices = range(next_pos, max_pos)
		sublists = map(_lists.__getitem__, sublist_indices)
		max_indices = range(max_idx)
		return chain(map(_lists[min_pos].__getitem__, min_indices), chain.from_iterable(sublists), map(_lists[max_pos].__getitem__, max_indices))

	def irange(self, minimum=None, maximum=None, inclusive=(True, True), reverse=False):
		_maxes = self._maxes
		if not _maxes:
			return iter(())
		_lists = self._lists
		if minimum is None:
			min_pos = 0
			min_idx = 0
		elif inclusive[0]:
			min_pos = bisect_left(_maxes, minimum)
			if min_pos == len(_maxes):
				return iter(())
			min_idx = bisect_left(_lists[min_pos], minimum)
		else:
			min_pos = bisect_right(_maxes, minimum)
			if min_pos == len(_maxes):
				return iter(())
			min_idx = bisect_right(_lists[min_pos], minimum)
		if maximum is None:
			max_pos = len(_maxes) - 1
			max_idx = len(_lists[max_pos])
		elif inclusive[1]:
			max_pos = bisect_right(_maxes, maximum)
			if max_pos == len(_maxes):
				max_pos -= 1
				max_idx = len(_lists[max_pos])
			else:
				max_idx = bisect_right(_lists[max_pos], maximum)
		else:
			max_pos = bisect_left(_maxes, maximum)
			if max_pos == len(_maxes):
				max_pos -= 1
				max_idx = len(_lists[max_pos])
			else:
				max_idx = bisect_left(_lists[max_pos], maximum)
		return self._islice(min_pos, min_idx, max_pos, max_idx, reverse)

	def __len__(self):
		return self._len

	def bisect_left(self, value):
		_maxes = self._maxes
		if not _maxes:
			return 0
		pos = bisect_left(_maxes, value)
		if pos == len(_maxes):
			return self._len
		idx = bisect_left(self._lists[pos], value)
		return self._loc(pos, idx)

	def bisect_right(self, value):
		_maxes = self._maxes
		if not _maxes:
			return 0
		pos = bisect_right(_maxes, value)
		if pos == len(_maxes):
			return self._len
		idx = bisect_right(self._lists[pos], value)
		return self._loc(pos, idx)
	bisect = bisect_right
	_bisect_right = bisect_right

	def count(self, value):
		_maxes = self._maxes
		if not _maxes:
			return 0
		pos_left = bisect_left(_maxes, value)
		if pos_left == len(_maxes):
			return 0
		_lists = self._lists
		idx_left = bisect_left(_lists[pos_left], value)
		pos_right = bisect_right(_maxes, value)
		if pos_right == len(_maxes):
			return self._len - self._loc(pos_left, idx_left)
		idx_right = bisect_right(_lists[pos_right], value)
		if pos_left == pos_right:
			return idx_right - idx_left
		right = self._loc(pos_right, idx_right)
		left = self._loc(pos_left, idx_left)
		return right - left

	def copy(self):
		return self.__class__(self)
	__copy__ = copy

	def append(self, value):
		raise NotImplementedError('use ``sl.add(value)`` instead')

	def extend(self, values):
		raise NotImplementedError('use ``sl.update(values)`` instead')

	def insert(self, index, value):
		raise NotImplementedError('use ``sl.add(value)`` instead')

	def pop(self, index=-1):
		if not self._len:
			raise IndexError('pop index out of range')
		_lists = self._lists
		if index == 0:
			val = _lists[0][0]
			self._delete(0, 0)
			return val
		if index == -1:
			pos = len(_lists) - 1
			loc = len(_lists[pos]) - 1
			val = _lists[pos][loc]
			self._delete(pos, loc)
			return val
		if 0 <= index < len(_lists[0]):
			val = _lists[0][index]
			self._delete(0, index)
			return val
		len_last = len(_lists[-1])
		if -len_last < index < 0:
			pos = len(_lists) - 1
			loc = len_last + index
			val = _lists[pos][loc]
			self._delete(pos, loc)
			return val
		(pos, idx) = self._pos(index)
		val = _lists[pos][idx]
		self._delete(pos, idx)
		return val

	def index(self, value, start=None, stop=None):
		_len = self._len
		if not _len:
			raise ValueError('{0!r} is not in list'.format(value))
		if start is None:
			start = 0
		if start < 0:
			start += _len
		if start < 0:
			start = 0
		if stop is None:
			stop = _len
		if stop < 0:
			stop += _len
		if stop > _len:
			stop = _len
		if stop <= start:
			raise ValueError('{0!r} is not in list'.format(value))
		_maxes = self._maxes
		pos_left = bisect_left(_maxes, value)
		if pos_left == len(_maxes):
			raise ValueError('{0!r} is not in list'.format(value))
		_lists = self._lists
		idx_left = bisect_left(_lists[pos_left], value)
		if _lists[pos_left][idx_left] != value:
			raise ValueError('{0!r} is not in list'.format(value))
		stop -= 1
		left = self._loc(pos_left, idx_left)
		if start <= left:
			if left <= stop:
				return left
		else:
			right = self._bisect_right(value) - 1
			if start <= right:
				return start
		raise ValueError('{0!r} is not in list'.format(value))

	def __add__(self, other):
		values = reduce(iadd, self._lists, [])
		values.extend(other)
		return self.__class__(values)
	__radd__ = __add__

	def __iadd__(self, other):
		self._update(other)
		return self

	def __mul__(self, num):
		values = reduce(iadd, self._lists, []) * num
		return self.__class__(values)
	__rmul__ = __mul__

	def __imul__(self, num):
		values = reduce(iadd, self._lists, []) * num
		self._clear()
		self._update(values)
		return self

	def __make_cmp(seq_op, symbol, doc):

		def comparer(self, other):
			if not isinstance(other, Sequence):
				return NotImplemented
			self_len = self._len
			len_other = len(other)
			if self_len != len_other:
				if seq_op is eq:
					return False
				if seq_op is ne:
					return True
			for (alpha, beta) in zip(self, other):
				if alpha != beta:
					return seq_op(alpha, beta)
			return seq_op(self_len, len_other)
		seq_op_name = seq_op.__name__
		comparer.__name__ = '__{0}__'.format(seq_op_name)
		doc_str = 'Return true if and only if sorted list is {0} `other`.\n        ``sl.__{1}__(other)`` <==> ``sl {2} other``\n        Comparisons use lexicographical order as with sequences.\n        Runtime complexity: `O(n)`\n        :param other: `other` sequence\n        :return: true if sorted list is {0} `other`\n        '
		comparer.__doc__ = dedent(doc_str.format(doc, seq_op_name, symbol))
		return comparer
	__eq__ = __make_cmp(eq, '==', 'equal to')
	__ne__ = __make_cmp(ne, '!=', 'not equal to')
	__lt__ = __make_cmp(lt, '<', 'less than')
	__gt__ = __make_cmp(gt, '>', 'greater than')
	__le__ = __make_cmp(le, '<=', 'less than or equal to')
	__ge__ = __make_cmp(ge, '>=', 'greater than or equal to')
	__make_cmp = staticmethod(__make_cmp)

	@recursive_repr()
	def __repr__(self):
		return '{0}({1!r})'.format(type(self).__name__, list(self))

	def _check(self):
		try:
			assert self._load >= 4
			assert len(self._maxes) == len(self._lists)
			assert self._len == sum((len(sublist) for sublist in self._lists))
			for sublist in self._lists:
				for pos in range(1, len(sublist)):
					assert sublist[pos - 1] <= sublist[pos]
			for pos in range(1, len(self._lists)):
				assert self._lists[pos - 1][-1] <= self._lists[pos][0]
			for pos in range(len(self._maxes)):
				assert self._maxes[pos] == self._lists[pos][-1]
			double = self._load << 1
			assert all((len(sublist) <= double for sublist in self._lists))
			half = self._load >> 1
			for pos in range(0, len(self._lists) - 1):
				assert len(self._lists[pos]) >= half
			if self._index:
				assert self._len == self._index[0]
				assert len(self._index) == self._offset + len(self._lists)
				for pos in range(len(self._lists)):
					leaf = self._index[self._offset + pos]
					assert leaf == len(self._lists[pos])
				for pos in range(self._offset):
					child = (pos << 1) + 1
					if child >= len(self._index):
						assert self._index[pos] == 0
					elif child + 1 == len(self._index):
						assert self._index[pos] == self._index[child]
					else:
						child_sum = self._index[child] + self._index[child + 1]
						assert child_sum == self._index[pos]
		except:
			import sys
			import traceback
			traceback.print_exc(file=sys.stdout)
			print('len', self._len)
			print('load', self._load)
			print('offset', self._offset)
			print('len_index', len(self._index))
			print('index', self._index)
			print('len_maxes', len(self._maxes))
			print('maxes', self._maxes)
			print('len_lists', len(self._lists))
			print('lists', self._lists)
			raise

def identity(value):
	return value
(n, m) = map(int, input().split())
s = [int(i) for i in input().split()]
a = SortedList()
a.add(s[0])
f = False
for i in range(1, n):
	a.add(s[i])
	j = 0
	curh = 0
	if i % 2 != 0:
		while j <= i:
			if j == i:
				curh += a[j]
			else:
				curh += a[j + 1]
			j += 2
			if curh > m:
				break
		if curh > m:
			break
	else:
		while j <= i:
			if j == i:
				curh += a[j]
			else:
				curh += a[j + 1]
			j += 2
			if curh > m:
				break
		if curh > m:
			curh = a[0]
			j = 1
			while j <= i:
				if j == i:
					curh += a[j]
				else:
					curh += a[j + 1]
				j += 2
				if curh > m:
					break
			if curh > m:
				break
else:
	f = True
	print(n)
if not f:
	print(len(a) - 1)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
while sum(sorted(a[:n])[-1::-2]) > h:
	n -= 1
print(n)


(n, h) = [int(x) for x in input().split()]
ns = [int(x) for x in input().split()]

def check(ns):
	ns.sort()
	i = len(ns) - 1
	cur = 0
	while i > 0:
		cur += ns[i]
		if cur > h:
			return False
		i -= 2
	if len(ns) % 2 == 1:
		cur += ns[0]
	if cur > h:
		return False
	return True
if check(ns[:]):
	print(n)
	quit()
(l, r) = (1, n)
while r - l > 1:
	m = (r + l) // 2
	if check(ns[:m]):
		l = m
	else:
		r = m
print(l)


(n, h) = map(int, input().split())
l = [*map(int, input().split())]
k = 0
while h >= sum(sorted(l[:k + 1])[-1::-2]) and k < n:
	k += 1
print(k)


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
from heapq import heapify, heappop, heappush, heappushpop, heapreplace

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline().rstrip()

def lsi():
	return list(si())
mod = 1000000007
res = ['Yes', 'No']

def solve(n):
	p = sorted(a[:n], reverse=True)
	s = i = 0
	while i < n:
		s += p[i]
		if s <= k:
			i += 1
			if i < n:
				i += 1
		else:
			return False
	return True

def bin_search():
	(pos, l, r) = (0, 0, len(a) - 1)
	while l <= r:
		m = (l + r) // 2
		if solve(m + 1):
			pos = m + 1
			l = m + 1
		else:
			r = m - 1
	return pos
test_case = 1
while test_case:
	test_case -= 1
	(n, k) = mi()
	a = li()
	print(bin_search())


(bottles_number, fridge_height) = [int(x) for x in input().split(' ')]
heights = [int(x) for x in input().split(' ')]

def teste(heights, i):
	new_list = list(heights[:i])
	new_list.sort()
	new_list.reverse()
	list_part = new_list[::2]
	if sum(list_part) <= fridge_height:
		return True
for i in range(len(heights) + 1):
	if not teste(heights, i):
		break
	else:
		aux = i
print(aux)


f = lambda : map(int, input().split())
(n, k) = f()
a = [*f()]
while sum(sorted(a[:n])[-1::-2]) > k:
	n -= 1
print(n)


(n, h) = [int(x) for x in input().split()]
nums = [int(x) for x in input().split()]
(lb, rb) = (0, n + 1)
while rb - lb > 1:
	mb = lb + rb >> 1
	if mb == n + 1:
		rb = mb
		continue
	bottles = sorted(nums[:mb], reverse=True)
	stat = sum((bottles[i] for i in range(0, len(bottles), 2)))
	if stat <= h:
		lb = mb
	else:
		rb = mb
print(lb)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
used = []
don = False
for i in range(n):
	if used == []:
		used.append(a[i])
	else:
		done = False
		for j in range(i):
			if a[i] < used[j]:
				used = used[:j] + [a[i]] + used[j:]
				done = True
				break
		if not done:
			used.append(a[i])
	if sum(used[-1::-2]) > h:
		print(i)
		don = True
		break
if not don:
	print(n)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
k = 1
f = True
while f and k <= len(a):
	b = sorted(a[:k], key=lambda x: -x)
	s = 0
	for i in range(0, len(b), 2):
		s += b[i]
	if s > h:
		f = False
		k -= 1
	else:
		k += 1
if k > len(a):
	print(len(a))
else:
	print(k)


first = [int(i) for i in input().split(' ')]
n = first[0]
h = first[1]
bottles = [int(i) for i in input().split(' ')]
res = 0

def check(i, sub, h):
	if i % 2 == 1:
		t = sum([sub[x] for x in range(0, i, 2)])
	else:
		t = sum([sub[x] for x in range(1, i + 1, 2)])
	if t <= h:
		return True
	return False
for i in range(0, n):
	sub = bottles[:i + 1]
	sub.sort()
	flag = check(i + 1, sub, h)
	if not flag:
		break
	res += 1
print(res)


(n, h) = map(int, input().split())
A = list(map(int, input().split()))
k = n
while k > 0:
	B = []
	for i in range(k):
		B.append(A[i])
	B.sort(reverse=True)
	sum = 0
	for i in range(0, len(B), 2):
		sum += B[i]
	if sum <= h:
		print(k)
		break
	k -= 1


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	b = a[:i + 1]
	b.sort(reverse=True)
	height = 0
	for j in range(len(b)):
		if j % 2 == 0:
			height += b[j]
	if height > h:
		break
	else:
		ans = i + 1
print(ans)


import heapq
(n, h) = map(int, input().split())
heights = [-int(x) for x in input().split()]
if n <= 2:
	print(n)
else:
	j = 2
	y = 0
	k_list = []
	while True:
		heights_small = heights[:j]
		heapq.heapify(heights_small)
		heapq.heappush(heights_small, heights[j])
		k = a = 0
		for i in range(0, j + 1, 2):
			z = -heapq.heappop(heights_small)
			if z <= h - a:
				if i == j:
					k += 1
				else:
					heapq.heappop(heights_small)
					k += 2
					a += z
			else:
				y += -1000
				break
		if y == -1000 or j == n - 1:
			k_list.append(k)
			break
		else:
			j += 1
			k_list.append(k)
	print(max(k_list))


(n, h) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
for i in range(n):
	b = sorted(a[:i + 1])
	if len(b) & 1 == 1 and h < sum([b[i] for i in range(0, len(b), 2)]):
		print(i)
		exit()
	elif len(b) & 1 == 0 and h < sum([b[i] for i in range(1, len(b), 2)]):
		print(i)
		exit()
print(n)


def check(k):
	g = h
	x = sorted(l[:k + 1], reverse=True)
	for i in range(0, k + 1, 2):
		if g < x[i]:
			return False
		g -= x[i]
	return True
(n, h) = list(map(int, input().split()))
l = list(map(int, input().split()))
for j in range(n - 1, -1, -1):
	if check(j):
		print(j + 1)
		break


def ispossible(x, h):
	c = 0
	for i in range(len(x) - 1, -1, -2):
		c += x[i]
	return c <= h
ll = lambda : list(map(int, input().split()))
[n, h] = ll()
l = ll()
for i in range(n, 0, -1):
	x = l[:i + 1]
	x.sort()
	if ispossible(x, h):
		print(len(x))
		break


def test(a):
	return sum(sorted(a)[::-2])
(n, h) = map(int, input().split())
d = list(map(int, input().split()))
if d[0] > h:
	print(0)
elif test(d) <= h:
	print(n)
else:
	(l, r) = (1, n)
	while r - l > 1:
		k = (r + l) // 2
		if test(d[:k]) <= h:
			l = k
		else:
			r = k
	print(l)


3

def can(a, h):
	a.sort(reverse=True)
	cur = 0
	for i in range(0, len(a), 2):
		cur += a[i]
	return cur <= h
(n, h) = list(map(int, input().split()))
a = list(map(int, input().split()))
(lt, rt) = (0, n + 1)
while lt < rt - 1:
	mid = (lt + rt) // 2
	if can(a[:mid], h):
		lt = mid
	else:
		rt = mid
print(lt)


def main():
	hist = {}
	for (k, a_i) in enumerate(a):
		if a_i not in hist:
			hist[a_i] = 0
		hist[a_i] += 1
		current_height = 0
		carryover = None
		for (h_i, count) in reversed(sorted(hist.items())):
			cnt = count
			if carryover:
				cnt -= 1
			current_height += h_i * (cnt // 2)
			if cnt % 2 != 0:
				current_height += h_i
				carryover = h_i
			else:
				carryover = None
			if current_height > h:
				return k
	return n
(n, h) = map(int, input().split())
a = list(map(int, input().split()))
print(main())


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

def can(mid):
	val = sum(sorted(a[:mid])[::-2])
	return val <= h
le = 1
rg = n + 1
while rg - le > 1:
	mid = (le + rg) // 2
	if can(mid):
		le = mid
	else:
		rg = mid
print(le)


f = lambda : map(int, input().split())
((n, k), a) = (f(), [*f()])
print([i for i in range(n + 1) if sum(sorted(a[:i])[::-2]) <= k][-1])


R = lambda : map(int, input().split())
(n, h) = R()
a = list(R())
y = []
k = 1
for i in range(1, n):
	y = a[:i + 1]
	y.sort(reverse=True)
	s = sum(y[::2])
	if h >= s:
		k = i + 1
	else:
		break
print(k)


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
tr = lambda m, x: sum([m[i] for i in range(0, x + 1, 2)])
N = 10 ** 9 + 7
(n, h) = R()
a = list(R())
l = 0
r = n - 1
ans = 0
while l <= r:
	m = (l + r) // 2
	x = tr(sorted(a[:m + 1], reverse=True), m)
	if x <= h:
		ans = m + 1
		l = m + 1
	else:
		r = m - 1
print(ans)


import math
(n, hei) = map(int, input().split())
a = list(map(int, input().split()))
l = 0
h = n - 1
while l < h:
	mid = math.ceil((l + h) / 2)
	b = a[:mid + 1]
	b.sort(reverse=True)
	x = 0
	for i in range(math.ceil(len(b) / 2)):
		x += b[2 * i]
	if x <= hei:
		l = mid
	else:
		h = mid - 1
if a[l] <= hei:
	print(l + 1)
else:
	print(0)


(n, h) = map(int, input().split())
ar = [int(x) for x in input().split()]
arr = [ar[0]]
ans = 1
for k in range(1, n):
	for p in range(len(arr)):
		flag = True
		if ar[k] >= arr[p]:
			arr.insert(p, ar[k])
			flag = False
			break
	if flag:
		arr.append(ar[k])
	temp = 0
	for p in range(0, len(arr), 2):
		temp += arr[p]
	if temp <= h:
		ans = len(arr)
print(ans)


from collections import defaultdict as dd
import math
import copy

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))
(n, k) = mi()
milk = lm()
bottles = 0
start = 0
end = n
while start <= end:
	mid = (start + end) // 2
	section = copy.copy(milk[:mid])
	height = 0
	section.sort()
	if mid % 2 == 0:
		for i in range(mid // 2):
			height += section[i * 2 + 1]
	if mid % 2 == 1:
		for i in range(mid // 2 + 1):
			height += section[i * 2]
	if height > k:
		end = mid - 1
	else:
		start = mid + 1
print(end)


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

def f(right):
	return sum(sorted(a[0:right])[::-2]) <= h
r = 0
while r < n and f(r + 1):
	r += 1
print(r)


((n, k), arr) = (map(int, input().split()), list(map(int, input().split())))
print(sum([sum(sorted(arr[:i])[::-2]) <= k for i in range(n + 1)]) - 1)


(n, h) = map(int, input().split())
A = list(map(int, input().split()))
for i in range(len(A)):
	A[i] = 0 - A[i]
from heapq import *
ans = 0
curheap = []
for a in A:
	newheap = []
	heappush(curheap, a)
	ind = 0
	curh = 0
	v = True
	while curheap != []:
		e = heappop(curheap)
		if ind % 2 == 0:
			curh += abs(e)
			if curh > h:
				v = False
				break
		heappush(newheap, e)
		ind += 1
	if v == False:
		break
	curheap = newheap
	ans += 1
print(ans)


([n, k], arr) = ([int(i) for i in input().split()], [int(i) for i in input().split()])
print([i for i in range(n + 1) if sum(sorted(arr[0:i])[::-2]) <= k][-1])


from sys import stdin
input = stdin.readline
(n, h) = map(int, input().split())
a = list(map(int, input().split()))
(left, right) = (0, n)
res = 0
while left <= right:
	mid = (left + right) // 2
	candidate = sorted(a[0:mid], reverse=True)
	t = sum(candidate[0::2])
	if t <= h:
		res = max(res, mid)
		left = mid + 1
	else:
		right = mid - 1
print(res)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return tuple(map(int, sys.stdin.readline().split()))

def LLI():
	return [tuple(map(int, l.split())) for l in sys.stdin]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def bs(f, mi, ma):
	mm = -1
	while ma > mi:
		mm = (ma + mi) // 2
		if f(mm):
			mi = mm + 1
		else:
			ma = mm
	if f(mm):
		return mm + 1
	return mm

def main():
	(n, h) = LI()
	a = LI()

	def f(i):
		if i > n:
			return False
		b = sorted(a[:i])
		t = None
		if i % 2 == 1:
			t = sum(b[::2])
		else:
			t = sum(b[1::2])
		return t <= h
	r = bs(f, 0, n + 1)
	return r - 1
print(main())


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n + 1):
	t = sum(sorted(a[:i])[::-2])
	if t > h:
		print(i - 1)
		break
else:
	print(n)


def canPut(hts, k, h):
	hts_temp = hts[:k]
	hts_temp.sort()
	if len(hts_temp) == 1:
		return True
	total = 0
	count = 0
	if k % 2 != 0:
		count += 1
		total += hts_temp[0]
	i = k - 1
	while i > count:
		total += max(hts_temp[i], hts_temp[i - 1])
		if total > h:
			return False
		i -= 2
	if total > h:
		return False
	return True

def main():
	(n, h) = map(int, input().split())
	hts = list(map(int, input().split()))
	for k in range(1, n + 1):
		if not canPut(hts, k, h):
			print(k - 1)
			return
	print(n)
main()


def mp():
	return map(int, input().split())
(n, h) = mp()
a = list(mp())
for i in range(1, n + 1):
	c = a[:i]
	c.sort(reverse=True)
	p = 0
	for j in range(0, i, 2):
		p += c[j]
	if p > h:
		print(i - 1)
		break
else:
	print(n)


(n, h) = map(int, input().split())
arr = list(map(int, input().split()))
use = []
for i in range(n):
	use.append(arr[i])
	use.sort()
	real_use = use[-1:-(i + 2):-2]
	if sum(real_use) > h:
		print(i)
		return
print(n)


import sys

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def check(n, x):
	a = sorted(arr[:n], reverse=True)
	c = 0
	for i in range(n):
		if c == 0:
			if a[i] > x:
				return False
			c = 1 - c
		else:
			if a[i] <= h:
				x -= max(a[i - 1], a[i])
			else:
				return False
			c = 1 - c
	return True

def main():
	global h, arr
	(n, h) = get_ints()
	arr = get_array()
	low = 0
	high = n
	while low < high:
		mid = (low + high + 1) // 2
		if check(mid, h) == True:
			low = mid
		else:
			high = mid - 1
	print(low)
main()


(n, d) = map(int, input().split())
a = list(map(int, input().split()))
max = 0
for i in range(n):
	l = a[:i + 1]
	l.sort(reverse=True)
	s = 0
	for j in range(0, i + 1, 2):
		s += l[j]
	if s <= d:
		if i + 1 > max:
			max = i + 1
	else:
		break
print(max)


(n, k) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
heights = []
ok = 0
for jj in range(n + 1):
	heights = sorted(A[:jj], reverse=True)
	totalheight = 0
	for i in range(len(heights)):
		if i & 1:
			continue
		else:
			totalheight += heights[i]
	if totalheight <= k:
		ok = jj
	else:
		break
print(ok)


(n, h) = map(int, input().split())
l = list(map(int, input().split()))
result = 1
for i in range(1, n):
	index = i
	part = sorted(l[0:index + 1])
	r = 0
	end = part[index]
	maks = end
	r += 2
	kalan = h
	if index >= 2:
		while True:
			kalan -= maks
			index -= 2
			if index >= 0:
				maks = part[index]
				if maks <= kalan:
					if index != 0:
						r += 2
					else:
						r += 1
				else:
					break
			else:
				break
	result = max(result, r)
print(result)


arr = input()
(N, H) = [int(x) for x in arr.split(' ')]
arr = input()
arr = [int(x) for x in arr.split(' ')]
res = 0
for i in range(N):
	d = sorted(arr[:i + 1], reverse=True)
	s = 0
	for i in range(len(d)):
		if i % 2 == 0:
			s += d[i]
	if s <= H:
		res = i + 1
	else:
		break
print(res)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.append(9999999999999)
st = 0
end = n + 1
while True:
	mid = (st + end) // 2
	b = a[:mid]
	b.sort()
	b.reverse()
	curs = 0
	for i in range(len(b)):
		if i % 2 == 0:
			curs += b[i]
	if st == end and curs > m:
		break
	if curs <= m:
		st = mid + 1
	else:
		end = mid - 1
	if st > end:
		break
print(st - 1)


import bisect
(n, h) = map(int, input().split())
b = [*map(int, input().split())]
t = [b[0]]
for i in range(1, n):
	bisect.insort(t, b[i])
	if sum(t[i % 2::2]) > h:
		print(i)
		exit()
print(n)


from bisect import insort
(n, h) = map(int, input().split())
a = list(map(int, input().split()))
li = []
for i in range(n):
	insort(li, a[i])
	j = len(li) - 1
	c = 0
	while j >= 0:
		c += li[j]
		j -= 2
	if c > h:
		print(i)
		break
else:
	print(n)


(n, h) = map(int, input().split())
c = list(map(int, input().split()))
i = n
c2 = sorted(c)
if i % 2 == 1:
	s = sum(c2[0:i:2])
else:
	s = sum(c2[1:i:2])
while s > h:
	i -= 1
	c = c[:i]
	c2 = sorted(c)
	if i % 2 == 1:
		s = sum(c2[0:i:2])
	else:
		s = sum(c2[1:i:2])
print(i)


def is_possible(num):
	global h, array
	copy = array[:num]
	copy = sorted(copy)
	res = h
	for i in range(len(copy) - 1, -1, -2):
		res -= copy[i]
	return res >= 0
(amount, h) = [int(s) for s in input().split()]
array = [int(s) for s in input().split()]
left = -1
right = int(10 ** 18)
while right - left > 1:
	middle = (right + left) // 2
	if is_possible(middle):
		left = middle
	else:
		right = middle
if left > amount:
	print(amount)
else:
	print(left)


s = input().split()
v = input().split()
holod = []
k = int(s[0])
h = int(s[1])
for i in range(0, k):
	holod.append(int(v[i]))
	ho = sorted(holod)
	m_h = 0
	for z in range(0, len(ho), 2):
		m_h += ho[len(ho) - z - 1]
	if h >= m_h:
		count = len(holod)
	else:
		break
print(count)


(n, h) = map(int, input().split())
heights = [int(x) for x in input().split()]
if n <= 2:
	print(n)
else:
	j = 1
	y = 0
	k_list = []
	while True:
		heights_small = sorted(heights[:j], reverse=True)
		k = a = 0
		for i in range(0, len(heights_small), 2):
			if heights_small[i] <= h - a:
				if i == j - 1:
					k += 1
				else:
					k += 2
					a += heights_small[i]
			else:
				y += -1000
				break
		if y == -1000 or j == n:
			k_list.append(k)
			break
		else:
			j += 1
			k_list.append(k)
	print(max(k_list))


import bisect

def final():
	tempH = 0
	for i in range(1, len(ta) + 1, 2):
		try:
			tempH += max(ta[-i], ta[-i - 1])
		except IndexError:
			tempH += ta[-i]
	return tempH
(n, hh) = map(int, input().split())
a = list(map(int, input().split()))
ta = list()
flag = 1
for i in range(n):
	bisect.insort(ta, a[i])
	if final() > hh:
		print(i)
		flag = 0
		break
if flag:
	print(n)


def summ(l):
	res = 0
	i = len(l) - 1
	while i >= 0:
		res += l[i]
		i -= 2
	return res
(n, h) = map(int, input().split())
b = list(map(int, input().split()))
m = n
for i in range(1, n + 1):
	if summ(sorted(b[0:i])) > h:
		m = i - 1
		break
print(m)


(n, h) = map(int, input().split())
a = [int(x) for x in input().split()]
ans = 1
for i in range(n):
	total = 0
	b = a[:i + 1]
	b.sort(reverse=True)
	for j in range(0, len(b), 2):
		total += b[j]
	if total <= h:
		ans = max(ans, len(b))
print(ans)


(a, c) = map(int, input().split())
b = list(map(int, input().split()))
mx = 1
for i in range(1, a):
	t = b[0:i + 1]
	t.sort(reverse=True)
	q = c
	for j in range(0, len(t), 2):
		q -= t[j]
	if q >= 0:
		mx = i + 1
print(mx)


(n, h) = map(int, input().split())
c = list(map(int, input().split()))
k = 0
for i in range(n):
	s = sorted(c[0:i + 1])[::-1]
	if sum(s[::2]) > h:
		k = 1
		break
if k == 0:
	print(n)
else:
	print(i)


(num_bottles, fridge_height) = [int(x) for x in input().split()]
bottle_heights = [int(x) for x in input().split()]
bottle_area = 0
i = 0
while True:
	if bottle_area > fridge_height * 2:
		i -= 1
		bottle_area = bottle_area - bottle_heights[i]
		break
	elif i < num_bottles:
		bottle_area += bottle_heights[i]
		i += 1
	else:
		i -= 1
		break
bottle_heights = bottle_heights[:i + 1]
sorted_bottles = sorted(bottle_heights, reverse=True)

def reverse_bisect(a, x, lo=0, hi=None):
	if hi is None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if x > a[mid]:
			hi = mid
		else:
			lo = mid + 1
	return lo - 1

def check_can_fit(bottle_array, height):
	shelf_height_sum = 0
	for x in range(0, len(bottle_array), 2):
		shelf_height_sum += bottle_array[x]
	if shelf_height_sum > height:
		return False
	else:
		return True
while True:
	if check_can_fit(sorted_bottles, fridge_height):
		print(len(sorted_bottles))
		exit()
	else:
		last_bottle_index = reverse_bisect(sorted_bottles, bottle_heights[-1])
		sorted_bottles.pop(last_bottle_index)
		bottle_heights.pop(-1)


import bisect
import atexit
import io
import sys
_I_B = sys.stdin.read().splitlines()
input = iter(_I_B).__next__
_O_B = io.StringIO()
sys.stdout = _O_B

@atexit.register
def write():
	sys.__stdout__.write(_O_B.getvalue())

def calc():
	global tl
	hh = 0
	for i in range(1, len(tl) + 1, 2):
		try:
			hh += max(tl[-i], tl[-i - 1])
		except IndexError:
			hh += tl[-i]
	return hh
(n, h) = map(int, input().split())
a = list(map(int, input().split()))
tl = []
for i in range(n):
	bisect.insort(tl, a[i])
	if calc() > h:
		print(i)
		exit(0)
print(n)


exec('n,k,*a=' + '*map(int,input().split()),' * 2)
while sum(sorted(a[:n])[::-2]) > k:
	n -= 1
print(n)


(n, h) = map(int, input().split())
arr = [int(x) for x in input().split()]
mx = 0
for i in range(1, n + 1):
	brr = arr[:i]
	brr.sort(reverse=True)
	height = 0
	for j in range(0, i, 2):
		height += brr[j]
	if height > h:
		break
	mx = i
print(mx)


(n, h) = map(int, input().strip().split())
bottles = [int(i) for i in input().strip().split()]
import heapq
b_list = []
can = True

def the_sort(the_list, k):
	new_list = []
	insert = 0
	need_sort = False
	for i in range(len(the_list)):
		if the_list[i] < k:
			insert = i
			need_sort = True
			break
	index = insert
	if not need_sort:
		the_list.append(k)
		return
	current = k
	while index < len(the_list):
		temp = the_list[index]
		the_list[index] = current
		current = temp
		index += 1
	the_list.append(temp)
for i in range(n):
	max_k = i
	the_sort(b_list, bottles[i])
	largest = b_list[0]
	index = 0
	remain = h
	while index <= i:
		remain -= largest
		if remain < 0:
			can = False
			break
		index += 1
		if index < i:
			index += 1
		else:
			break
		largest = b_list[index]
	if not can:
		break
print(max_k if not can else n)


class Solution:

	def __init__(self, n, h, a):
		i = 1
		for z in range(n):
			b = []
			for j in range(z + 1):
				b.append(int(a[j]))
			b.sort(reverse=True)
			len = h
			k = 0
			for j in range(int((i + 1) / 2)):
				len -= b[k]
				k += 2
				if len < 0:
					print(i - 1)
					return
			i += 1
		print(n)
data = input().split()
n = int(data[0])
h = int(data[1])
a = input().split()
Solution(n, h, a)


(n, h) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
ans = 0
for i in range(len(a)):
	arr = a[:i + 1]
	arr.sort()
	ht = 0
	for j in range(len(arr) - 1, -1, -2):
		ht += max(arr[j], arr[max(0, j - 1)])
	if ht <= h:
		ans = max(ans, i + 1)
print(ans)


(n, h) = [int(i) for i in input().split(' ')]
a = [int(i) for i in input().split(' ')]
print([i + 1 for i in range(n) if sum(sorted(a[:i + 1])[::-1][::2]) <= h][-1])


p = input().rstrip().split(' ')
q = input().rstrip().split(' ')
n = int(p[1])
s = 0
l = []
S = []
D = []
if len(q) == 1 and int(q[0]) <= n:
	print(1)
elif len(q) == 2:
	if int(q[0]) <= n and int(q[1]) <= n:
		print(2)
	elif int(q[0]) <= n and int(q[1]) > n:
		print(1)
	else:
		print(0)
else:
	if int(q[0]) <= n:
		l.append(int(q[0]))
		s = s + 1
	if int(q[1]) <= n:
		l.append(int(q[1]))
		s = s + 1
	for i in range(2, len(q)):
		l.append(int(q[i]))
		l.sort(key=int, reverse=True)
		Q = list(l)
		P = 0
		for j in range(0, len(Q), 2):
			P = P + int(Q[j])
		if P <= n:
			s = s + 1
	print(s)


from sys import stdin, stderr
(n, h) = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))

def ok(k):
	b = list(sorted(a[:k], reverse=True))
	t = sum(b[::2])
	return t <= h
l = 0
r = n + 1
while r - l > 1:
	mid = (l + r) // 2
	if ok(mid):
		l = mid
	else:
		r = mid
print(l)


(N, H) = map(int, input().split())
A = [int(a) for a in input().split()]
for i in range(N + 1):
	B = sorted([a for a in A[:i]])[::-1]
	s = sum(B[::2])
	if s > H:
		print(i - 1)
		break
else:
	print(N)


def calculate(ls, h):
	Sum = 0
	temp = ls
	try:
		while True:
			Sum += max(temp[-1], temp[-2])
			temp = temp[:-2]
	except:
		if len(ls) % 2 == 1:
			Sum += ls[0]
	return Sum <= h
(n, h) = map(int, input().split())
ls = list(map(int, input().split()))
cnt = 0
for i in range(n):
	cnt += 1
	temp = sorted(ls[:cnt])
	if not calculate(temp, h):
		break
if not calculate(sorted(ls), h):
	print(cnt - 1)
else:
	print(n)


import sys, heapq
(n, h) = map(int, sys.stdin.readline().split())
arr = list(map(int, sys.stdin.readline().split()))
left = 0
right = 2 * n
while left < right:
	mid = (left + right) // 2
	temp = []
	for i in range(min(mid, n)):
		heapq.heappush(temp, -1 * arr[i])
	now = h
	cnt = 0
	a = 0
	b = 0
	while temp:
		if a != 0 and b != 0:
			now -= max(a, b)
			a = 0
			b = 0
		if now >= -1 * temp[0]:
			cnt += 1
			if a == 0:
				a += -1 * temp[0]
			elif b == 0:
				b += -1 * temp[0]
			heapq.heappop(temp)
		else:
			break
	if cnt >= mid:
		left = mid + 1
	else:
		right = mid
print(right - 1)


from math import sqrt, ceil, floor
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
import string
import bisect

def insert(list, n):
	bisect.insort(list, n)
	return list
(n, h) = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
currents = []
ans = 0
cur_h = 0
for i in a:
	cur_h = 0
	insert(currents, i)
	l = False
	for j in reversed(range(0, len(currents))):
		if not l:
			cur_h += currents[j]
			l = True
		else:
			l = False
	if cur_h > h:
		break
	ans += 1
stdout.write('%d' % ans)


from collections import deque
(m, n) = map(int, input().split())
bottles = deque(list(map(int, input().split())))
chosen = []
sum = 0
while bottles and sum < 2 * n:
	chosen.append(bottles.popleft())
	sum += chosen[-1]
if sum > 2 * n:
	chosen.pop()
tmp = chosen.copy()
while True:
	chosen = tmp.copy()
	chosen.sort()
	count = len(chosen)
	h = 0
	while chosen and h < n:
		if len(chosen) == 1:
			x = chosen.pop()
			h += x
		else:
			x = chosen.pop()
			y = chosen.pop()
			h += max(x, y)
	if len(chosen) != 0 or h > n:
		tmp.pop()
	else:
		print(count)
		break


def f(a):
	a.sort()
	ans = 0
	for i in range(len(a) - 1, -1, -2):
		ans += a[i]
	return ans
(n, h) = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(1, n + 1):
	b = a[:i].copy()
	x = f(b)
	if x <= h:
		ans = i
print(ans)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
k = 0
for i in range(1, n + 1):
	b = a[:i]
	b = sorted(b)
	ans = 0
	for j in range(len(b) - 1, -1, -2):
		ans += b[j]
	if ans <= h:
		k = i
	else:
		break
print(k)


def b(n, h, a):
	v = 0
	w = n - 1
	while v < w:
		mid = (v + w + 1) // 2
		choose = a[:mid + 1]
		choose.sort()
		choose.reverse()
		height = 0
		for i in range(0, mid + 1, 2):
			height += choose[i]
		if height > h:
			w = mid - 1
		else:
			v = mid
	return v + 1
(n, h) = input().strip().split()
a = input().strip().split()
a = list(map(int, a))
print(b(int(n), int(h), a))


from sys import stdin
(n, h) = list(map(int, stdin.readline().split()))
a = list(map(int, stdin.readline().split()))
v = h * 2
sv = sum(a)
k = n
while sv > v:
	k -= 1
	sv -= a[k]
fit = 0
k += 1
while not fit:
	k -= 1
	ff = fr = 1
	ta = sorted(a[:k])
	sh = 0
	sf = 0
	th = 0
	fit = 1
	for b in ta:
		sf += 1
		th = max(th, sh + b)
		if th > h:
			ff = 0
			break
		if sf == 2:
			sh = th
			sf = 0
	ta = sorted(a[:k], reverse=True)
	sh = 0
	sf = 0
	th = 0
	fit = 1
	for b in ta:
		sf += 1
		th = max(th, sh + b)
		if th > h:
			fr = 0
			break
		if sf == 2:
			sh = th
			sf = 0
	fit = ff + fr
print(k)


(n, h) = [int(i) for i in input().split()]
bottles = [int(i) for i in input().split()]

def f(x):
	b = sorted(bottles[:x + 1], reverse=True)
	sum = 0
	for i in range(0, len(b), 2):
		sum += b[i]
	return sum <= h
l = 0
r = n
while l < r:
	m = (l + r) // 2
	if f(m):
		l = m + 1
	else:
		r = m
print(l)


(n, h) = map(int, input().split())
bottles = list(map(int, input().split()))
k = n
while k > 0:
	temp_h = h
	temp = bottles[:k]
	temp.sort(reverse=True)
	i = 0
	while i < k and temp[i] <= temp_h:
		temp_h -= temp[i]
		i += 2
	if i >= k:
		break
	k -= 1
print(k)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_str():
	return input().decode().strip()

def rint():
	return map(int, input().split())

def oint():
	return int(input())
(n, h) = rint()
a = list(rint())
for i in range(n):
	aa = a[:i + 1]
	aa.sort(reverse=True)
	cnt = 0
	for j in range(0, len(aa), 2):
		cnt += aa[j]
	if cnt > h:
		print(i)
		break
else:
	print(n)


mod = 10 ** 9 + 7
from sys import stdin, stdout
import bisect
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
import collections
import math
import heapq
from random import randint as rn

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def main():
	(n, k) = cin()
	l = ain()
	x = []
	ind = 0
	for i in l:
		x.append(i)
		x.sort(reverse=True)
		a = sum(x[::2])
		if a > k:
			break
		ind += 1
	print(ind)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(n, h) = map(int, input().split())
l = list(map(int, input().split()))
c = 0
for i in range(n):
	c += 1
	t = sorted(l[:c])
	S = i = 0
	try:
		while True:
			S += t[-1 - i]
			i += 2
	except:
		if S > h:
			break
t = sorted(l)
S = i = 0
try:
	while True:
		S += t[-1 - i]
		i += 2
except:
	if S <= h:
		print(n)
	else:
		print(c - 1)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n, 0, -1):
	part = list(sorted(a[0:i]))
	if len(part) % 2 == 0:
		part.insert(0, 0)
	v = sum(part[::2])
	if v <= h:
		print(i)
		break


def solve(n, h, ar):
	track = []
	ans = 0
	flag = False
	for k in range(n):
		track = sorted(ar[:k + 1], reverse=True)
		cnt = 0
		for i in range(0, len(track), 2):
			cnt += track[i]
			if cnt > h:
				flag = True
				break
			if i + 2 >= len(track):
				break
		if flag:
			break
		else:
			ans = k
	print(ans + 1)
(n, h) = map(int, input().split())
ar = list(map(int, input().split()))
solve(n, h, ar)


def I():
	return int(input())

def IL():
	return list(map(int, input().split()))

def IM():
	return map(int, input().split())

def IS():
	return input()

def ISL():
	return list(input())
(n, h) = IM()
l = IL()

def can_put(mi):
	if mi == 0:
		return l[0] <= h
	z = sorted(l[0:mi + 1])
	req = 0
	if mi % 2 == 0:
		req += z[0]
		for i in range(1, mi + 1, 2):
			req += max(z[i], z[i + 1])
	else:
		for i in range(0, mi + 1, 2):
			req += max(z[i], z[i + 1])
	return req <= h
lo = 0
hi = n - 1
ans = 0
while lo <= hi:
	mi = lo + hi >> 1
	if can_put(mi):
		ans = mi
		lo = mi + 1
	else:
		hi = mi - 1
print(ans + 1)


(n, h) = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
for i in range(n):
	ll = sorted(l[:i + 1])
	ll = ll[::-1]
	x = len(ll)
	d = 0
	for j in range(0, x):
		if j % 2:
			d += max(ll[j], ll[j - 1])
	if x % 2 == 1:
		d += ll[-1]
	if d <= h:
		ans = max(ans, i)
print(ans + 1)


def check(parts, h):
	parts = sorted(parts, reverse=True)
	while parts:
		m = parts[0]
		h -= m
		if h < 0:
			return False
		parts = parts[1:]
		if parts:
			parts = parts[1:]
		else:
			return True

def find_max(n, h, milk):
	for k in range(2, len(milk) + 1):
		if check(milk[:k], h) == False:
			return k - 1
	return n
(n, h) = map(int, input().split())
milk = list(map(int, input().split()))
print(find_max(n, h, milk))


(N, H) = map(int, input().split())
a_list = list(map(int, input().split()))
ans = 0
if a_list[0] > H:
	print(0)
	exit()
import bisect
que = [a_list[0]]
ans = 1
prev = []
now_h = 0
for (i, a) in enumerate(a_list[1:]):
	i += 1
	bisect.insort_right(que, a)
	tmp = que.copy()
	tmp = list(reversed(tmp))
	now_h = 0
	k = 0
	f = False
	while True:
		first = tmp[k]
		if now_h + first > H:
			break
		else:
			k += 2
			now_h += first
			if k > len(tmp) - 1:
				f = True
				break
	if f:
		ans += 1
	else:
		break
print(ans)


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

def check(k):
	if k == 0:
		return True
	ar = a[:k]
	ar = sorted(ar, key=lambda x: -x)
	s = 0
	for i in range((k + 1) // 2):
		m = ar[2 * i]
		s += m
	return s <= h
l = 0
r = len(a)
ans = 0
while r >= l:
	m = (r + l) // 2
	if check(m):
		ans = max(ans, m)
		l = m + 1
	else:
		r = m - 1
print(ans)


(n, h) = [int(x) for x in input().strip().split(' ')]
nh = [int(x) for x in input().strip().split(' ')]
if n == 1:
	print(n)
	exit()
used_h = 0
for i in range(0, n):
	sorted_n = sorted(nh[:i + 1])[::-1]
	idx = 0
	used_h = 0
	for j in range(0, len(sorted_n), 2):
		used_h += sorted_n[j]
	if used_h > h:
		print(i)
		break
if used_h <= h:
	print(n)


R = lambda : map(int, input().split())
(n, h) = R()
a = [*R()]
k = 0
while k < n and sum(sorted(a[:k + 1])[-1::-2]) <= h:
	k += 1
print(k)


import sys
from collections import defaultdict as dd
from itertools import permutations as pp
from itertools import combinations as cc
from collections import Counter as ccd
from random import randint as rd
from bisect import bisect_left as bl
from heapq import heappush as hpush
from heapq import heappop as hpop
mod = 10 ** 9 + 7

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())

def check(k):
	now = a[:k]
	now.sort(reverse=True)
	ok = 0
	f = 0
	for i in range(len(now)):
		f += 1
		if f % 2 == 1:
			ok += now[i]
	if ok <= h:
		return 1
	else:
		return 0
(n, h) = ri()
a = ri()
ans = -1
low = 0
high = n
while low <= high:
	mid = (low + high) // 2
	if check(mid):
		low = mid + 1
		ans = mid
	else:
		high = mid - 1
print(ans)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
l = 0
r = n + 1
while r - l > 1:
	x = (l + r) // 2
	q = a[:x].copy()
	q.sort()
	h1 = 0
	fl = 1
	for i in range(len(q) - 1, -1, -2):
		h1 += q[i]
		if h1 > h:
			fl = 0
			break
	if fl:
		l = x
	else:
		r = x
print(l)


(n, h) = input().split()
n = int(n)
h = int(h)
b = list(map(int, input().split()))
k = len(b)
while k != 0:
	b_srt = sorted(b[:k], reverse=True)
	if sum(b_srt[0::2]) <= h:
		print(k)
		break
	k -= 1


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
(l, r) = (1, n + 1)
while l + 1 < r:
	mid = l + r >> 1
	c = sorted(a[:mid], reverse=True)
	if sum((c[i] for i in range(len(c)) if i & 1 == 0)) <= h:
		l = mid
	else:
		r = mid
print(l)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
x = []
ans = 0
s = 0
for i in range(n):
	x.append(a[i])
	x.sort(reverse=True)
	s = 0
	for j in range(0, len(x), 2):
		s += x[j]
	if s <= m:
		ans = i + 1
	else:
		break
print(ans)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
	suba = sorted(a[:i + 1])
	if sum([suba[x] for x in range(i, -1, -2)]) > h:
		print(i)
		exit()
print(n)


import sys, os
from io import BytesIO, IOBase
mod = 10 ** 9 + 7
Mod = 998244353
INF = float('inf')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda : list(map(int, sys.stdin.readline().rstrip('\r\n').split()))
tc = 1
for test in range(1, tc + 1):
	(n, k) = inp()
	a = inp()
	ans = 0

	def valid(a, k):
		a.sort()
		i = len(a) - 1
		while i >= 0:
			k -= a[i]
			i -= 2
		return k >= 0
	for i in range(1, n + 1):
		if valid(a[:i], k):
			ans = i
	print(ans)


import bisect
(n, h) = map(int, input().split())
arr = list(map(int, input().split()))
if n == 1:
	print(1)
elif n == 2:
	print(2)
else:
	X = []
	flag = 0
	k = 0
	ans = 0
	for i in range(n):
		X.append(arr[i])
		X.sort()
		if len(X) >= 3:
			sm = 0
			for j in range(len(X) - 1, -1, -2):
				if sm + X[j] > h:
					flag = 1
					break
				sm += X[j]
		if flag == 1:
			ans = i
			break
		elif i == n - 1:
			ans = i + 1
	k = ans
	print(k)


import sys
stdout = sys.stdout
rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : list(map(int, rr().split()))

def solve(N, H, A):

	def possible(k):
		B = sorted((A[i] for i in range(k)), reverse=True)
		ans = 0
		for i in range(0, len(B), 2):
			ans += B[i]
		return ans <= H
	lo = 0
	hi = len(A)
	while lo < hi:
		mi = (lo + hi + 1) // 2
		if possible(mi):
			lo = mi
		else:
			hi = mi - 1
	return lo
(N, H) = rrm()
A = rrm()
print(solve(N, H, A))


def func(arr):
	arr.sort()
	ans = 0
	for i in range(len(arr) - 1, -1, -2):
		ans += arr[i]
	return ans

def main():
	(n, h) = map(int, input().split())
	arr = list(map(int, input().split()))
	for i in range(1, n + 1):
		if func(arr[:i]) > h:
			i -= 1
			break
	print(i)
	return 0
main()


import sys

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(self.next())

	def solve(self):
		n = self.next_int()
		h = self.next_int()
		x = [self.next_int() for _ in range(0, n)]
		low = 0
		high = n + 1
		while high > low + 1:
			mid = (high + low) // 2
			xx = sorted(x[:mid], reverse=True)
			hh = 0
			for i in range(0, mid, 2):
				hh += xx[i]
			if hh <= h:
				low = mid
			else:
				high = mid
		print(low)
Main().solve()


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(1, n + 1):
	tmp = sorted(a[:i], key=lambda w: -w)
	htmp = 0
	for j in range(0, i, 2):
		htmp += tmp[j]
	if htmp > h:
		break
	else:
		ans = i
print(ans)


from itertools import islice
(n, h) = map(int, input().split())
a = list(map(int, input().split()))
for k in range(n, 0, -1):
	if sum((el for el in islice(sorted(islice(a, 0, k), reverse=True), 0, k, 2))) <= h:
		print(k)
		break
else:
	print('0')


(n, h) = map(int, input().split())
v = [int(e) for e in input().split()]
(a, o) = ([], 0)
for i in range(n):
	a.append(v[i])
	a.sort()
	(b, f, j) = (0, True, i)
	while j > 0:
		if h - b >= a[j]:
			b += a[j]
		else:
			f = False
			break
		j -= 2
	if not i & 1 and h - b < a[0]:
		f = False
	if not f:
		break
	else:
		o = i + 1
print(o)


(a, c) = map(int, input().split())
A = list(map(int, input().split()))
k = False
b = a
while k == False:
	B = A[:b]
	B.sort()
	v = 0
	i = b - 1
	while i >= 0:
		v += B[i]
		i -= 2
	if v <= c:
		k = True
	else:
		b -= 1
print(b)


(n, h) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
flag = False
for i in range(n):
	arr = sorted(a[:i + 1], reverse=True)
	height = 0
	index = 0
	while index < i:
		if index == n:
			break
		height += max(arr[index], arr[index + 1])
		if height > h:
			height -= max(arr[index], arr[index + 1])
			height += min(arr[index], arr[index + 1])
			if height <= h:
				print(i)
				flag = True
				break
			print(i)
			flag = True
			break
		index += 2
	if not i % 2 and (not flag):
		height += arr[-1]
		if height > h:
			print(i)
			flag = True
	if flag:
		break
else:
	print(n)


(n, k, *a) = [int(i) for _ in '12' for i in input().split()]
while sum(sorted(a[:n])[::-2]) > k:
	n -= 1
print(n)


def possible(m):
	new = sorted(a[:m])
	if m % 2:
		return sum(new[::2]) <= h
	return sum(new[1::2]) <= h
(n, h) = map(int, input().split())
a = [int(x) for x in input().split()]
l = 0
r = n + 1
while r - l > 1:
	m = (l + r) // 2
	if possible(m):
		l = m
	else:
		r = m
print(l)


[n, h] = [int(i) for i in input().split(' ')]
a = [int(i) for i in input().split(' ')]
lo = 1
hi = n

def can(mid):
	b = [_ for _ in a[:mid]]
	b.sort()
	b.reverse()
	res = 0
	for i in range(0, mid, 2):
		res += b[i]
	return res <= h
while lo < hi - 1:
	mid = (lo + hi) // 2
	if can(mid):
		lo = mid
	else:
		hi = mid - 1
if can(hi):
	lo = hi
print(lo)


from sys import stdin
(n, h) = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
for k in range(1, n + 1):
	lst = sorted(a[:k])
	reqh = 0
	if len(lst) % 2:
		for i in range(0, len(lst), 2):
			reqh += lst[i]
	else:
		for i in range(1, len(lst), 2):
			reqh += lst[i]
	if reqh > h:
		print(k - 1)
		break
else:
	print(n)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
l = []
for i in range(n):
	l.append(a[i])
	l.sort(reverse=True)
	if sum(l[::2]) > h:
		print(i)
		break
else:
	print(n)


(n, h) = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
curr = 0
for i in range(n):
	p = l[:i + 1]
	p = sorted(p, reverse=True)
	count = 0
	for ii in range(0, i + 1, 2):
		count += p[ii]
	if count > h:
		print(curr)
		quit()
	curr += 1
print(n)


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

def check(m):
	f = 0
	b = a[0:m]
	b.sort()
	while len(b) != 0:
		if len(b) == 1:
			f += b[0]
			b.pop()
		else:
			f += b[len(b) - 1]
			b.pop()
			b.pop()
	return f <= h

def search(l, r):
	m = (l + r) // 2
	if l == m:
		return l
	if not check(m):
		return search(l, m)
	else:
		return search(m, r)
if check(n):
	print(n)
else:
	print(search(0, n))


from sys import setcheckinterval, stdin, setrecursionlimit
setcheckinterval(1000)
setrecursionlimit(10 ** 6)
iin = lambda : int(stdin.readline())
lin = lambda : list(map(int, stdin.readline().split()))
from heapq import *
(n, h) = lin()
a = lin()
x = []
heapify(x)
ch = 0
for i in a:
	ch += 1
	heappush(x, i)
	j = nlargest(ch + 1, x)[::2]
	if sum(j) > h:
		heappop(x)
		ch -= 1
		break
print(ch)


(n, h) = map(int, input().split())
a = [int(s) for s in input().split()]
hi = n + 1
lo = min(n, 2)
mi = (hi + lo) // 2
while hi > lo + 1:
	ar = a[:mi]
	ar.sort()
	h_need = sum([ar[i] for i in range(len(ar)) if (len(ar) - i) % 2 == 1])
	if h_need <= h:
		lo = mi
	else:
		hi = mi
	mi = (hi + lo) // 2
print(mi)


(qtd, tamanho) = list(map(int, input().split()))
leites = list(map(int, input().split()))
total = 0
for i in range(1, qtd + 1):
	leitesordenados = sorted(leites[0:i], reverse=True)
	if sum(leitesordenados[::2]) > tamanho:
		break
	total += 1
print(total)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
k = 0
cur = []
for i in range(n):
	cur.append(a[i])
	cur.sort()
	now = 0
	if len(cur) % 2 == 1:
		for j in range(0, len(cur), 2):
			now += cur[j]
	else:
		for j in range(1, len(cur), 2):
			now += cur[j]
	if now <= h:
		k = i + 1
print(k)


from collections import defaultdict as DD
from bisect import bisect_left as BL
from bisect import bisect_right as BR
from itertools import combinations as IC
from itertools import permutations as IP
from random import randint as RI
import sys
MOD = pow(10, 9) + 7

def IN(f=0):
	if f == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
(n, k) = IN()
a = IN()
T = []
M = n
for i in range(n):
	j = BL(T, a[i])
	T[j:j] = [a[i]]
	f = 0
	m = 0
	for g in range(len(T) - 1, -1, -2):
		m += T[g]
	if m > k:
		M = i
		break
print(M)


(n, h1) = map(int, input().split())
s = list(map(int, input().split()))
(l, r) = (1, n + 1)
while r - l >= 1:
	h = h1
	m = (l + r) // 2
	a = sorted(s[:m])
	for q in range(m - 2, -1, -2):
		h -= a[q + 1]
	if m % 2 == 1:
		h -= a[0]
	if h < 0:
		r = m
	else:
		l = m
	if r - l == 1:
		print(l)
		break


nh = input().split(' ')
n = int(nh[0])
h = int(nh[1])
hparcial = 0
garrafas = input().split(' ')
for i in range(len(garrafas)):
	garrafas[i] = int(garrafas[i])
l = []
for i in range(n):
	l.append(garrafas[i])
	l.sort()
	soma = 0
	for j in range(i % 2, i + 1, 2):
		soma = soma + l[j]
	if soma <= h:
		hparcial = i + 1
print(hparcial)


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
(n, h) = MAP()
arr = LIST()
l = []
res = []
for k in range(1, n + 1):
	for j in range(k):
		l.append(arr[j])
	l.sort(reverse=True)
	summ = 0
	for j in range(0, k, 2):
		summ += l[j]
	if summ <= h:
		res.append(k)
	l = []
print(max(res))


import sys
input = sys.stdin.readline
(n, h) = map(int, input().split())
a = list(map(int, input().split()))
l = []
for k in range(len(a)):
	l.append(a[k])
	l.sort(reverse=True)
	if sum(l[0::2]) > h:
		print(k)
		sys.exit(0)
print(n)


import sys
input = sys.stdin.readline
from bisect import *
(N, H) = map(int, input().split())
A = list(map(int, input().split()))
L = []
for i in range(N):
	insort(L, A[i])
	if sum(L[len(L) - 1::-2]) > H:
		print(i)
		exit()
else:
	print(N)


import copy
(m, p) = map(int, input().split())
z = list(map(int, input().split()))
result = 1
flag = True
for i in range(1, m + 1):
	n = copy.deepcopy(p)
	if flag == False:
		break
	a = copy.deepcopy(z[0:i])
	a.sort(reverse=True)
	for j in range(0, len(a), 2):
		if n - a[j] >= 0:
			n = n - a[j]
		else:
			result = i - 1
			flag = False
			break
if flag == True:
	print(m)
else:
	print(result)


def solve():
	(n, h) = [int(x) for x in input().split(' ')]
	a = [int(x) for x in input().split(' ')]

	def good(x):
		y = h
		if x > n:
			return False
		b = a[:x]
		b.sort()
		while b and y:
			z = b.pop()
			if b:
				b.pop()
			y -= z
		return not b and y >= 0
	l = 0
	r = n + 1
	while r - l > 1:
		m = (l + r) // 2
		if good(m):
			l = m
		else:
			r = m
	return l
print(solve())


from sys import stdin
(n, h) = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
l = 0
r = n
while l < r:
	mid = (l + r) // 2 + (l + r) % 2
	lst = sorted(a[:mid])
	reqh = 0
	if len(lst) % 2:
		for i in range(0, len(lst), 2):
			reqh += lst[i]
	else:
		for i in range(1, len(lst), 2):
			reqh += lst[i]
	if reqh > h:
		r = mid - 1
	else:
		l = mid
print(l)


import sys
from bisect import bisect_left, bisect_right
from collections import deque, Counter
inf = float('inf')
mod = 1000000007

def binary(number):
	result = 0
	while number:
		result = result + 1
		number = number & number - 1
	return result

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())
a = get_array()
(n, height) = (a[0], a[1])
bottles = get_array()
max_ = 0

def place_in_rack(bottles_old, height):
	bottles = sorted(bottles_old, reverse=True)
	ret = 0
	num_ = 0
	i = 0
	while i < len(bottles):
		if bottles[i] <= height:
			height -= bottles[i]
			if i < len(bottles) - 1:
				ret += bottles[i] + bottles[i + 1]
				num_ += 2
				i += 2
			else:
				ret += bottles[i]
				num_ += 1
				i += 1
		else:
			break
	return num_
for i in range(len(bottles)):
	num = place_in_rack(bottles[:i + 1], height)
	max_ = max(max_, num)
print(max_)


def ok(i):
	cp = sorted(ls[:i + 1], reverse=True)
	v = 0
	for j in range(0, i + 1, 2):
		v += cp[j]
	return v <= h
kk = lambda : map(int, input().split())
ll = lambda : list(kk())
(n, h) = kk()
ls = ll()
l = 0
r = n - 1
while l + 1 < r:
	mid = l + (r - l >> 1)
	if ok(mid):
		l = mid
	else:
		r = mid - 1
print(r + 1 if ok(r) else l + 1)


([n, K], arr) = ([int(i) for i in input().split()], [int(i) for i in input().split()])
count = 0
for k in range(1, n + 1):
	firstK = sorted(arr[0:k])
	sumK = sum(firstK[::-2])
	if sumK <= K:
		count = k
	else:
		break
print(count)


def check(arr, k):
	arr.sort()
	add = 0
	for i in range(len(arr) - 1, -1, -2):
		add += arr[i]
	if add <= k:
		return True
	else:
		return False

def main():
	(n, h) = map(int, input().split())
	a = list(map(int, input().split()))
	start = 0
	end = n - 1
	while start <= end:
		mid = (start + end) // 2
		if check(a[:mid + 1], h):
			ans = mid
			start = mid + 1
		else:
			end = mid - 1
	print(ans + 1)
	return
main()


(n, h) = list(map(int, input().split()))
bottles = list(map(int, input().split()))

def ch(l, n=2):
	for i in range(0, len(l), n):
		yield l[i:i + n]
win = 0
for k in range(len(bottles) + 1):
	b = sorted(bottles[:k], reverse=True)
	hight = 0
	for chunk in ch(b):
		hight += max(chunk)
	if hight <= h:
		win = k
	else:
		break
print(win)


(n, h) = map(int, input().split())
A = list(map(int, input().split()))
B = []
for (i, a) in enumerate(A):
	B.append(a)
	B.sort(reverse=True)
	s = 0
	for j in range(i + 1):
		if j % 2 == 0:
			s += B[j]
	if s > h:
		break
else:
	print(n)
	exit()
print(i)


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

def test(x):
	s = a[0:x + 1]
	s.sort(key=lambda v: -v)
	total_h = 0
	i = 0
	while i < x / 2:
		total_h += s[2 * i]
		i += 1
	if x % 2 == 0:
		total_h += s[x]
	return total_h
l = 0
r = n
while r - l > 1:
	c = (r + l) // 2
	if test(c) <= h:
		l = c
	else:
		r = c
print(l + 1)


(amF, h) = list(map(int, input().split()))
arrF = list(map(int, input().split()))
maxWas = 0
for amS in range(1, amF + 1):
	am = amS
	arr = sorted(arrF[:amS], reverse=True)
	hNow = 0
	ind = 0
	tot = 0
	while True:
		if ind >= am - (am & 1):
			break
		if hNow + arr[ind] <= h:
			tot += 1
			if hNow + arr[ind + 1] <= h:
				tot += 1
				hNow += max(arr[ind], arr[ind + 1])
				ind += 2
			else:
				break
		else:
			break
		if ind >= am - (am & 1):
			break
	if am & 1:
		if arr[ind] + hNow <= h:
			tot += 1
	maxWas = max(tot, maxWas)
print(maxWas)


(n, h) = input().strip().split(' ')
(n, h) = [int(n), int(h)]
a = list(map(int, input().strip().split(' ')))
ans = 0
for i in range(n + 1):
	p = sum(sorted(a[:i])[::-2])
	if p <= h:
		ans = i
	else:
		break
print(ans)


GI = lambda : int(input())
GIS = lambda : map(int, input().split())
LGIS = lambda : list(GIS())

def main():
	(n, ht) = GIS()
	a = LGIS()
	hs = []
	mf = 0
	while len(hs) < len(a):
		hs.append(a[len(hs)])
		hs.sort(reverse=True)
		c = 0
		for i in range(0, len(hs), 2):
			c += max(hs[i:i + 2])
		if c > ht:
			break
		mf += 1
	print(mf)
main()


(n, h) = [int(x) for x in input().split(' ')]
a = [int(x) for x in input().split(' ')]
m = n
for i in range(0, n):
	nok = False
	b = a[0:i + 1]
	b.sort()
	if i % 2 != 0:
		s = 0
		for j in range(1, i + 1, 2):
			if s + b[j] > h:
				nok = True
				break
			else:
				s += b[j]
	else:
		s = 0
		for j in range(0, i + 1, 2):
			if s + b[j] > h:
				nok = True
				break
			else:
				s += b[j]
	if nok:
		m = i
		break
print(m)


(n, h) = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
for k in range(n, 0, -1):
	temp = sorted(arr[:k], reverse=True)
	if sum(temp[::2]) <= h:
		print(k)
		break


def f(p):
	T = sorted(L[:p + 1], reverse=True)
	h1 = H
	for i in range(0, p + 1, 2):
		h1 -= T[i]
		if h1 < 0:
			return 0
	return 1
R = lambda : map(int, input().split())
(n, H) = R()
L = list(R())
(l, h) = (0, n - 1)
while l <= h:
	m = (l + h) // 2
	if f(m):
		l = m + 1
	else:
		h = m - 1
print(l)


(n, h) = list(map(int, input().split()))
hs = list(map(int, input().split()))

def is_filled(hs, n, h):
	hs = sorted(hs)
	result = 0
	for i in range(n):
		if i % 2 == 0:
			result += hs[n - 1 - i]
			if result > h:
				return False
	return True

def fill_fridge(hs, n, h):
	for i in range(3, n + 1):
		if is_filled(hs[:i], i, h) == False:
			return print(i - 1)
	return print(n)
fill_fridge(hs, n, h)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
v = []
ans = 0
for i in range(n):
	v.append(a[i])
	v.sort(reverse=True)
	cnt = 0
	for j in range(0, len(v), 2):
		cnt += v[j]
	if cnt <= h:
		ans = i + 1
print(ans)


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

def possible(k):
	S = list(sorted(a[:k]))
	if k % 2 == 0:
		return sum(map(max, zip(S[::2], S[1::2]))) <= h
	else:
		return sum(map(max, zip(S[2::2], S[1::2]))) + S[0] <= h
(l, r) = (0, n)
while l < r:
	m = (l + r >> 1) + 1
	if possible(m):
		l = m
	else:
		r = m - 1
print(l)


import sys
import copy
input = sys.stdin.readline
(n, h) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
b = []
for i in range(n):
	b.append(a[i])
	b.sort(reverse=True)
	sum = 0
	for j in range(0, i + 1, 2):
		sum += b[j]
	if sum <= h:
		ans = i + 1
print(ans)


(n, hh) = [int(x) for x in input().split(' ')]
aa = [int(x) for x in input().split(' ')]
(l, r) = (0, len(aa) + 1)
while r - l > 1:
	h = hh
	c = (l + r) // 2
	a = sorted(aa[:c])
	if len(a) % 2 == 1:
		a = [0] + a
	ans = True
	i = 0
	for i in range(len(a) // 2):
		if h < a[2 * i + 1]:
			ans = False
		h -= a[2 * i + 1]
	if ans == True:
		l = c
	else:
		r = c
print(l)


def getInput():
	temp = input().split(' ')
	(n, h) = (int(temp[0]), int(temp[1]))
	heights = input().split(' ')
	for i in range(len(heights)):
		heights[i] = int(heights[i])
	return (n, h, heights)

def fits(i):
	height_left = h
	p = 0
	while p < i:
		if heights[p] <= height_left:
			height_left -= heights[p]
			p += 1
			if p < i:
				p += 1
		else:
			return False
	return True
(n, h, heights) = getInput()
k = n
for i in range(1, n + 1):
	heights[:i] = sorted(heights[:i], reverse=True)
	if fits(i):
		continue
	else:
		k = i - 1
		break
print(k)


(n, h) = [int(i) for i in input().split(' ')]
a = [int(i) for i in input().split(' ')]

def cal(cur):
	if len(cur) % 2 == 0:
		return sum(cur[1:len(cur):2])
	else:
		return sum(cur[0:len(cur):2])

def helper(n, h, a):
	(l, r) = (0, len(a) + 1)
	while r - l > 1:
		mid = l + (r - l >> 1)
		if cal(sorted(a[:mid])) <= h:
			l = mid
		else:
			r = mid
	print(l)
helper(n, h, a)


(n, h) = [int(j) for j in input().split()]
a = [int(j) for j in input().split()]
b = [0 for j in range(n)]
if a[0] <= h:
	b[0] = a[0]
d = 0
e = -1
for i in range(1, n):
	d = 0
	j = i - 1
	key = a[i]
	while j >= 0 and key < b[j]:
		b[j + 1] = b[j]
		j -= 1
	b[j + 1] = key
	if (i + 1) % 2 == 0:
		for j in range(1, i + 1, 2):
			d = d + b[j]
	else:
		for j in range(0, i + 1, 2):
			d = d + b[j]
	if d > h:
		e = i
		break
if e == -1:
	print(n)
else:
	print(e)


def f(n, h, arr):
	now = []
	rez = 0
	for i in range(n):
		now.append(arr[i])
		now.sort(reverse=True)
		now_h = 0
		for j in range(0, len(now), 2):
			try:
				second = now[j + 1]
			except Exception:
				second = 0
			now_h += max(now[j], second)
		if now_h > h:
			return rez + 1
		else:
			rez = i
	return rez + 1
(n, h) = list(map(int, input().split()))
arr = list(map(int, input().split()))
print(f(n, h, arr))


[n, h] = list(map(int, input().split()))
a = list(map(int, input().split()))
for sol in range(n, -1, -1):
	b = a[0:sol]
	b.sort()
	s = 0
	for i in range(len(b) - 1, -1, -2):
		s += b[i]
	if s <= h:
		break
print(sol)


__author__ = 'r0levrai'
from math import *
from operator import itemgetter
(n, h) = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
for k in range(1, n + 1):
	test_A = sorted(A[:k])
	test_A.reverse()
	test_k = 0
	hleft = h
	leftslot = 0
	for i in range(len(test_A)):
		a = test_A[min(i + 1, len(test_A) - 1)] if i % 2 == 0 else test_A[i - 1]
		if (hleft - a if leftslot == 0 else hleft - a + leftslot) >= 0:
			test_k += 1
			hleft = hleft - a if leftslot == 0 else hleft - a + leftslot
			leftslot = 0 if leftslot != 0 else a
		else:
			break
	if test_k < k:
		print(k - 1)
		break
else:
	print(k)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
n += 1
a.append(1000000000)
for i in range(n):
	s = 0
	lst = sorted(a[:i + 1])
	ln = len(lst)
	for j in range(ln - 1, -1, -2):
		s += lst[j]
	if s > h:
		print(i)
		break


(n, h) = map(int, input().split())
arr = [int(x) for x in input().split()]
for i in range(n, 0, -1):
	brr = sorted(arr[:i], key=lambda x: -x)
	height_sum = 0
	for j in range(0, i, 2):
		height_sum += brr[j]
	if height_sum <= h:
		print(i)
		break


def func(l, h):
	l.sort(reverse=True)
	ln = len(l)
	(ols, els) = (0, 0)
	if ln % 2 == 0:
		for i in range(0, ln, 2):
			els += l[i]
		if els <= h:
			return True
		return False
	else:
		for i in range(0, ln - 1, 2):
			ols += l[i]
		ols += l[-1]
		if ols <= h:
			return True
		return False
(n, h) = map(int, input().split())
arr = list(map(int, input().split()))
for i in range(n - 1, -1, -1):
	if func(arr[:i + 1], h):
		print(i + 1)
		break


(n, h) = input().split()
n = int(n)
h = int(h)
l = list(map(int, input().split()))
k = []
r = 0
for i in range(n):
	k.append(l[i])
	k.sort(reverse=True)
	ans = 0
	for j in range(0, len(k), 2):
		ans = ans + k[j]
	if ans <= h:
		r = i + 1
	else:
		break
print(r)


(n, h) = list(map(int, input().split()))
a = list(map(int, input().split()))
for i in range(n):
	a1 = a[:i + 1]
	a1.sort()
	h1 = 0
	for j in range(0, len(a1), 2):
		ai = a1[len(a1) - j - 1]
		h1 += ai
		if h1 > h:
			break
	if h1 > h:
		print(i)
		break
else:
	print(n)


(n, h) = map(int, input().split())
a = [int(_) for _ in input().split()]
res = 1
for k in range(n):
	part = a[:k + 1]
	part.sort()
	(ans, ans2) = (0, 0)
	if len(part) & 1:
		for i in range(0, k + 1, 2):
			ans += part[i]
		for i in range(1, k + 1, 2):
			ans2 += part[i]
		ans2 += part[-1]
		ans = min(ans, ans2)
	else:
		for i in range(1, k + 1, 2):
			ans += part[i]
	if ans <= h:
		res = max(res, len(part))
	else:
		break
print(res)


(n, h) = list(map(int, input().split()))
l = list(map(int, input().split()))

def poss(k):
	tab = sorted(l[:k])
	suma = 0
	i = k - 1
	while i >= 0:
		suma += tab[i]
		i -= 2
	if suma > h:
		return False
	return True
lo = 0
hi = n
kand = 0
while True:
	if poss((lo + hi) // 2):
		lo = (lo + hi) // 2
	else:
		hi = (lo + hi) // 2
	if abs(lo - hi) < 2:
		kand = lo
		break
if poss(kand + 1):
	print(kand + 1)
else:
	print(kand)


from sys import stdin
(n, m) = map(int, stdin.readline().strip().split())
s1 = list(map(int, stdin.readline().strip().split()))

def can(s):
	ans = 0
	x = 0
	h = 0
	s.sort()
	s = s[::-1]
	while x < len(s):
		if x == len(s) - 1:
			a = s[x]
			if h + a <= m:
				ans += 1
				h = h + a
			else:
				return False
			x += 1
		else:
			a = min(s[x], s[x + 1])
			b = max(s[x + 1], s[x])
			if h + b <= m:
				ans += 2
				h += b
			else:
				return False
			x += 2
	return True
ans = 0
for i in range(1, n + 1):
	if can(s1[0:i]):
		ans += 1
	else:
		break
print(ans)


(n, h) = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
answer = 0
for i in range(1, n + 1):
	a = c[:i]
	a.sort(reverse=True)
	summa = 0
	for item in range(0, i, 2):
		summa += a[item]
	if summa > h:
		print(answer)
		break
	else:
		answer = i
else:
	print(answer)


(n, h) = map(int, input().split())
A = list(map(int, input().split()))
bot = []
for i in range(n):
	cnt = 0
	bot.append(A[i])
	bot.sort(reverse=True)
	for iq in range(0, len(bot), 2):
		cnt += bot[iq]
	if cnt > h:
		print(i)
		return
print(n)


(n, h) = map(int, input().split())
c = list(map(int, input().split()))
w = []
for i in range(n + 1):
	if sum(sorted(c[0:i])[::-2]) <= h:
		w.append(i)
print(w[-1])


(n, h) = tuple(map(int, input().split()))
a = list(map(int, input().split()))
ans = True
for i in range(n):
	x = a[0:i + 1]
	x.sort(reverse=True)
	t = 0
	for j in range(0, len(x), 2):
		t += x[j]
	if t > h:
		ans = False
		break
print(i + ans)


def solve(n, h, a):
	bt = [(hi, i) for (i, hi) in sorted(enumerate(a), key=lambda item: item[1])]
	k = n
	while k > 0:
		i = n - 1
		hleft = h
		while i > 0:
			(bh, bi) = bt[i]
			if bi < k:
				hleft -= bh
				if hleft < 0:
					break
				i -= 1
				while i > 0 and bt[i][1] >= k:
					i -= 1
				if hleft == 0:
					break
			i -= 1
		if i <= 0:
			return k
		k -= 1
	return 1
(n, h) = (int(p) for p in input().split())
a = [int(p) for p in input().split()]
print(solve(n, h, a))


import sys
nh = input().split()
n = int(nh.pop(0))
h = int(nh.pop(0))
a = [int(i) for i in input().split()]
ans = 1
hi = n
lo = 1
while lo <= hi:
	mid = int((hi + lo) / 2)
	b = a
	b = sorted(b[0:mid]) + b[mid:]
	m = 0
	for j in range(mid - 1, 0, -2):
		if j - 1 >= 0:
			m += b[j]
	if mid % 2:
		m += b[0]
	if m <= h:
		ans = max(ans, mid)
		lo = mid + 1
	else:
		hi = mid - 1
print(ans)


(n, k) = map(int, input().split())
s = list(map(int, input().split()))
print([i for i in range(n + 1) if sum(sorted(s[0:i])[::-2]) <= k][-1])


import sys
sys.setrecursionlimit(10 ** 5 + 1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
import sys
sys.setrecursionlimit(10 ** 5 + 1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
import bisect
a = []
(nb_bottles, max_height) = RMI()
answer = 0
heights = RMI()
for ht in heights:
	bisect.insort(a, ht)
	if sum(a[::-2]) > max_height:
		break
	answer += 1
print(answer)


inputs = [int(x) for x in input().split()]
n = inputs[0]
k = inputs[1]
a = [int(x) for x in input().split()]
newArray = []
for i in range(n):
	m = i
	for j in range(i):
		if a[i] <= newArray[j]:
			m = j
			break
	newArray = newArray[:m] + [a[i]] + newArray[m:]
	sumRows = 0
	if i % 2 == 1:
		for r in range(int(i / 2) + 1):
			sumRows += newArray[2 * r + 1]
	else:
		for r in range(int(i / 2) + 1):
			sumRows += newArray[2 * r]
	if sumRows > k:
		print(i)
		break
	if i == n - 1:
		print(n)


from sys import stdin, stdout
from bisect import bisect_right as br
from bisect import bisect_left as bl
from bisect import insort_left as il
from math import gcd
check = []
ans = 0
t = 0
(n, h) = map(int, stdin.readline().split())
lst = [int(i) for i in stdin.readline().split()]
for i in lst:
	il(check, i)
	k = 0
	for j in range(t, len(check), 2):
		k += check[j]
	if k > h:
		break
	ans += 1
	t += 1
	t %= 2
stdout.write(str(ans))


(a, b) = map(int, input().split())
z = list(map(int, input().split()))
s = 0
for i in range(a + 1):
	if sum(sorted(z[:i])[::-2]) <= b:
		s = i
print(s)


def check(subarray):
	ret = 0
	l = len(subarray)
	for i in range(l - 1, -1, -2):
		ret = ret + subarray[i]
	return ret
(n, h) = input().split(' ')
n = int(n)
h = int(h)
a = list(map(int, input().split(' ')))
l = len(a)
for i in range(l, 0, -1):
	subarray = a[:i]
	subarray.sort()
	if check(subarray) <= h:
		print(len(subarray))
		exit()


(n, h) = map(int, input().split())
bottles = list(map(int, input().split()))
out = 0
for middle in range(n, -1, -1):
	choice = bottles[:middle]
	choice = sorted(choice, reverse=True)
	total_bottles = 0
	current_h = 0
	i = 0
	while i < middle and current_h + choice[i] <= h:
		current_h += choice[i]
		i += 2
		total_bottles += 2
	total_bottles = min(total_bottles, middle)
	if total_bottles == middle:
		print(total_bottles)
		break


(n, h) = [int(i) for i in input().split()]
alturas = [int(i) for i in input().split()]
answer = 0
for i in range(1, n + 1):
	lista = alturas[0:i]
	lista.sort(reverse=True)
	novaLista = lista[::2]
	if sum(novaLista) <= h:
		answer = i
print(answer)


(n, h) = map(int, input().split())
mas = list(map(int, input().split()))
ans = 0
for i in range(n):
	m = sorted(mas[0:i + 1])[::-1]
	ch = 0
	for j in range(0, i, 2):
		ch += max(m[j], m[j + 1])
	if i % 2 == 0:
		ch += m[-1]
	if ch <= h:
		ans += 1
	else:
		print(ans)
		break
else:
	print(n)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
l = 0
r = n - 1
while l < r:
	mid = (l + r + 1) // 2
	choose = a[:mid + 1]
	choose.sort(reverse=True)
	height = 0
	for i in range(0, mid + 1, 2):
		height += choose[i]
	if height > h:
		r = mid - 1
	else:
		l = mid
print(l + 1)


def fridge(n, h, lst):
	(count, temp) = (0, 0)
	for i in range(n):
		s = sorted(lst[0:i + 1])[::-1]
		if sum(s[::2]) > h:
			(count, temp) = (1, i)
			break
	if count == 0:
		return n
	return temp
(N, H) = [int(i) for i in input().split()]
a = [int(j) for j in input().split()]
print(fridge(N, H, a))


import math
import bisect
import heapq

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, x, y) = egcd(b % a, a)
		return (g, y - b // a * x, x)

def mulinv(b, n):
	(g, x, _) = egcd(b, n)
	if g == 1:
		return x % n
primes = []

def isprime(n):
	for d in range(2, int(math.sqrt(n)) + 1):
		if n % d == 0:
			return False
	return True

def argsort(ls):
	return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
	if p == 1:
		return map(int, input().split())
	elif p == 2:
		return list(map(int, input().split()))
	else:
		return int(input())
(n, h) = f(1)
cl = f(2)
x = [cl[0]]
mx = 1
for i in range(1, n):
	bisect.insort(x, cl[i])
	sm = 0
	for j in range(i - 1, -1, -2):
		sm += max(x[j], x[j + 1])
		if sm >= h:
			break
	if i % 2 == 0:
		sm += x[0]
	if sm <= h:
		mx = max(mx, i + 1)
	else:
		break
print(mx)


from sys import stdin
data = stdin.read().split('\n')
(n, h) = map(int, data[0].split())
arr = [int(x) for x in data[1].split()]
for k in range(1, n + 1):
	s = sum(sorted(arr[:k], key=lambda x: -x)[0::2])
	if s > h:
		k -= 1
		break
print(k)


(n, b) = list(map(int, input().split()))
a = list(map(int, input().split()))
st = 0
end = n
e = 0
while st <= end:
	mid = (st + end) // 2
	c = sorted(a[:mid], reverse=True)
	if len(c) % 2 != 0:
		c.append(0)
	s = 0
	for i in range(0, len(c), 2):
		s += max(c[i], c[i + 1])
	ans = -1
	if s == b:
		ans = mid
		e += 1
		if e == 1:
			if mid < n:
				c = sorted(a[:mid + 1], reverse=True)
				if len(c) % 2 != 0:
					c.append(0)
				s = 0
				for i in range(0, len(c), 2):
					s += max(c[i], c[i + 1])
				if s == b:
					ans = mid + 1
					break
				else:
					ans = mid
					break
			break
	elif s > b:
		end = mid - 1
	else:
		st = mid + 1
if ans == -1:
	ans = end
print(ans)


(n, h) = map(int, input().split())
s = []
for i in map(int, input().split()):
	s = sorted(s + [i])
	if sum(s[-1::-2]) > h:
		exit(print(len(s) - 1))
print(n)


(n, h) = map(int, input().split())
a = list(map(int, input().split()))
maxi = 0
for i in range(n + 1):
	new = sorted(a[0:i])
	if sum(new[::-2]) <= h:
		maxi = i
	else:
		break
print(maxi)


(n, h) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
k = 0
K = 0
for i in range(n):
	k += 1
	t = a[:k]
	t.sort(reverse=True)
	num = 0
	height = 0
	good = True
	while num < k:
		height += t[num]
		num += 2
		if height > h:
			good = False
	if good == False:
		break
	K = k
print(K)


import math
from bisect import insort
from collections import deque, defaultdict
from sys import stdin, stdout
input = stdin.readline
listin = lambda : list(map(int, input().split()))
mapin = lambda : map(int, input().split())
(n, h) = mapin()
a = listin()
s = []
for i in a:
	insort(s, i)
	if sum(s[n - 1::-2]) > h:
		print(len(s) - 1)
		exit()
print(n)


from math import *
from itertools import *
(n, h) = map(int, input().split(' '))
k = h
b = list(map(int, input().split(' ')))
ans = 0
flg = 0
for j in range(n):
	a = sorted(b[:j + 1])
	a = a[::-1]
	c = 0
	i = 0
	h = k
	n = len(a)
	while i < n:
		if h <= 0:
			flg = 1
			break
		if a[i] <= h:
			h -= a[i]
			c += 1
		else:
			flg = 1
		i += 1
		if i < n:
			c += 1
		i += 1
	ans = max(ans, c)
	if flg == 1:
		break
print(ans)


def find_proper_amount():
	for i in range(bottle_amount):
		partial_bottles = bottles[:i + 1]
		if check_height_exceed(partial_bottles):
			return i
	return bottle_amount

def check_height_exceed(bottles):
	sorted_bottles = sorted(bottles)[::-1]
	total_height = 0
	for i in range(0, len(sorted_bottles), 2):
		total_height += sorted_bottles[i]
	if total_height > friged_height:
		return True
	return False
(bottle_amount, friged_height) = tuple(map(int, input().split()))
bottles = list(map(int, input().split()))
print(find_proper_amount())


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	(n, h) = sep()
	a = lis()
	for i in range(n, -1, -1):
		new = sorted(a[:i])[::-1]
		hii = 0
		for j in range(0, i, 2):
			hii += new[j]
		if hii <= h:
			print(i)
			return
testcase(1)


def main():
	(n, h) = map(int, input().split())
	a = list(map(int, input().split()))
	(left, right, ret) = (1, n, 1)
	while left <= right:
		mid = (left + right) // 2
		temp = a[:mid]
		temp.sort(reverse=True)
		curr_height = 0
		for i in range(0, mid, 2):
			curr_height += temp[i]
		if curr_height <= h:
			ret = max(ret, mid)
			left = mid + 1
		else:
			right = mid - 1
	print(ret)
main()


import sys
[h, n] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
ans = 0
for i in range(h + 1):
	if sum(sorted(a[:i])[::-2]) > n:
		break
	ans = i
print(ans)


h = input().split(' ')
n = int(h[0])
h = int(h[1])
s = input().split(' ')
mas = []
summa = 0
for i in range(n):
	summa += int(s[i])
	if summa <= h * 2:
		mas.append(int(s[i]))
	else:
		break
i = 0
f = True
while f:
	temp = h
	if i == 0:
		k = mas.copy()
	else:
		k = mas[:-i].copy()
	k.sort(reverse=True)
	for j in range(0, len(k), 2):
		temp -= k[j]
	if temp < 0:
		i += 1
		continue
	else:
		f = False
		break
print(len(k))


import sys
(n, h) = map(int, input().split())
l = list(map(int, sys.stdin.readline().split()))
c = l.copy()
i = 0
c.sort(reverse=True)
T = float('infinity')
while T > h:
	T = sum([c[k] for k in range(0, n - i, 2)])
	c.pop(c.index(l[-i - 1]))
	i += 1
print(n + 1 - i)


(n, h) = list(map(int, input().split()))
l = list(map(int, input().split()))
for i in range(n):
	l[i] = [l[i], i]
l.sort()
mb = 0
for j in range(n):
	cnt1 = 0
	cnt2 = 0
	tmp = []
	mus = 0
	for i in range(n):
		if l[i][1] <= j:
			tmp.append(l[i][0])
			mus += tmp[-1]
	for i in range(j + 1):
		if cnt1 >= cnt2 and cnt2 + tmp[i] <= h:
			cnt2 += tmp[i]
		elif cnt1 + tmp[i] <= h:
			cnt1 += tmp[i]
	if mus != cnt1 + cnt2:
		print(j)
		exit()
	if j == n - 1 and mus == cnt1 + cnt2:
		print(j + 1)
		exit()


def can(k):
	u = sorted(a[:k])[::-1]
	if sum(u) > 2 * h:
		return False
	r = h
	i = 0
	while i < k:
		if i == k - 1:
			return u[k - 1] <= r
		if u[i] > r:
			return False
		r -= u[i]
		i += 2
	return True
(n, h) = [int(item) for item in input().split()]
a = [int(item) for item in input().split()]
(l, r) = (1, n + 1)
while l < r:
	m = l + r >> 1
	if can(m):
		l = m + 1
	else:
		r = m
print(l - 1)


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

def is_possible(h, a):
	cur_h = 0
	for i in range(0, len(a), 2):
		bottle_height = a[i]
		if cur_h + bottle_height > h:
			return False
		cur_h += bottle_height
	return True
(l, r) = (0, n)
solution = 0
while l <= r:
	k = (l + r) // 2
	new_a = list(sorted(a[:k], reverse=True))
	if is_possible(h, new_a):
		l = k + 1
		solution = k
	else:
		r = k - 1
print(solution)


def can_arrange(heights, h):
	heights = sorted(heights, reverse=True)
	th = 0
	for i in range(0, len(heights), 2):
		th += heights[i]
	return th <= h
(n, h) = (int(x) for x in input().split())
idx___height = [int(x) for x in input().split()]
l_cl = 0
r_op = n + 1
while r_op - l_cl > 1:
	m = (l_cl + r_op) // 2
	if can_arrange(idx___height[:m], h):
		l_cl = m
	else:
		r_op = m
print(l_cl)