#C9873. Maximize Deliveries

    ID: 54014 Type: Default 1000ms 256MiB

Maximize Deliveries

Maximize Deliveries

You are given a set of delivery packages, each with a weight and a deadline. A delivery agent has a weight capacity W and can deliver each package in one unit of time. Packages must be delivered in the order of their deadlines. Your task is to select a subset of packages so that their total weight does not exceed W and the number of delivered packages is maximized.

Note: Although each package has a deadline, they only affect the delivery order. By sorting the packages by their deadlines and then choosing packages according to the available weight capacity, you can guarantee that each selected package is delivered on time.

The problem requires you to process several test cases.

Constraints:

  • For each test case, the first line contains two integers: M (the number of packages) and W (the maximum weight capacity).
  • The following M lines each contain two integers: the weight Wi and the deadline Di of a package.
  • The input terminates with a line containing "0 0" which should not be processed.

Your goal is to compute the maximum number of packages that can be delivered for each test case.

Mathematical Formulation:

Given a test case with packages \( (W_1, D_1), (W_2, D_2), \dots, (W_M, D_M) \) and capacity \( W \), let \( S \subseteq \{1,2,\dots,M\} \) be the indices of the selected packages such that \( \sum_{i \in S} W_i \leq W \). You need to maximize \( |S| \) subject to the above constraint. The packages are delivered in non-decreasing order of their deadlines \( D_i \).

inputFormat

The input is read from standard input (stdin) and consists of several test cases. Each test case is formatted as follows:

M W
W1 D1
W2 D2
... 
WM DM

The test cases are given consecutively and the input terminates with a line containing "0 0".

outputFormat

For each test case, print one integer on a separate line to standard output (stdout) representing the maximum number of packages that can be delivered without exceeding the weight capacity.

## sample
3 5
3 5
2 2
1 1
4 10
1 2
2 4
3 6
4 8
0 0
2

4

</p>