#K4631. Max Books on Shelf

    ID: 27948 Type: Default 1000ms 256MiB

Max Books on Shelf

Max Books on Shelf

You are given a shelf of length (m) and (n) books. Each book has a width and a height, but only the width is used to determine if it fits on the shelf. The objective is to choose as many books as possible such that the sum of their widths does not exceed (m). The books should be arranged in increasing order of their widths, and then picked greedily until the shelf is full.

Formally, given a sequence of widths (w_1, w_2, \dots, w_n), sort them such that (w_{i1} \leq w_{i2} \leq \dots \leq w_{in}) and find the maximum integer (k) such that (\sum_{j=1}^{k} w_{ij} \leq m).

inputFormat

The input is provided via standard input (stdin). The first line contains two space-separated integers (n) and (m), where (n) is the number of books and (m) is the shelf length. Each of the following (n) lines contains two integers denoting the width and height of a book. Note that the height is provided but is not required for solving this problem.

outputFormat

Output a single integer to standard output (stdout) representing the maximum number of books that can be placed on the shelf without exceeding its length.## sample

5 10
1 3
2 5
3 1
4 2
2 4
4