// Arup Guha
// 10/3/2021
// Solution to 2021 COP 3502 Fall 2021 Program #4, using Quick Sort
// Edited to handle one case only for Spring 2022 Version (3/23/2022)

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

// All the functions we need to sort.
void quickSort(int* nums, int len);
void quickSortRec(int* nums, int low, int high);
int same(int* array, int low, int high);
int partition(int* nums, int low, int high);
void swap(int* array, int idx1, int idx2);

int main(void) {

    int n;
    long long maxPages;

    // Get # of books and most # of pages.
    scanf("%d%lld", &n, &maxPages);

    // Read in the # of pages in each book.
    int* books = malloc(sizeof(int)*n);
    for (int i=0; i<n; i++)
        scanf("%d", &books[i]);

    // Sort in order.
    quickSort(books, n);

    // Set both of these to 0.
    int res = 0;
    long long total = 0;

    // Sweep through books, smallest to largest, trying to read them.
    while (res < n) {

        // We can't read this one, get out.
        if (total + books[res] > maxPages) break;

        // Update pages read and books read.
        total += books[res];
        res++;
    }

    // Ta da!
    printf("%d\n", res);

    return 0;
}

// Sorts the array nums of length n (wrapper function).
void quickSort(int* nums, int len) {
    quickSortRec(nums, 0, len-1);
}

void quickSortRec(int* nums, int low, int high) {

    // No work left.
    if (low >= high) return;

    // Ensures faster run time for arrays with many repeated values.
    if (same(nums, low, high)) return;

    // Partition the array.
    int part = partition(nums, low, high);

    // Recursively sort left of the partition.
    quickSortRec(nums, low, part-1);

    // And right of the partition.
    quickSortRec(nums, part+1, high);
}

// Pre-condition: 0 <= low < high < length(nums)
// Post-condition: Partitions array so that all values less than or equal to
//                 array[part] are in indexes low to part-1 and all
//                 values greater than array[part] are in indexes
//                 part+1 through high and returns part.
int partition(int* nums, int low, int high) {

    // Pick a random partition element and swap it into index low.
    int swapElem = low + rand()%(high-low+1);
    swap(nums, low, swapElem);

    // Set my two pointers.
    int i = low+1, j = high;

    // Sweep till they cross over.
    while (i <= j) {

        // Find first element on left that's too big.
        while (i<=high && nums[i]<=nums[low]) i++;

        // Find first element on right that's too small.
        while (j>=low && nums[j]>nums[low]) j--;

        // Elements need to be swapped.
        if (i < j) swap(nums, i, j);
    }

    // Swap partition element into the right place.
    swap(nums, low, j);

    // This is where the partition element now is.
    return j;
}

// Swaps array[idx1] and array[idx2].
void swap(int* array, int idx1, int idx2) {
    int tmp = array[idx1];
    array[idx1] = array[idx2];
    array[idx2] = tmp;
}

// Returns 1 iff all the values in array[low..high] are the same.
int same(int* array, int low, int high) {

    // Return false if any value differs from the first.
    for (int i=low+1; i<=high; i++)
        if (array[low] != array[i])
            return 0;

    // If we get here, they were all the same.
    return 1;
}
