#K14431. Maximum Products Delivery

    ID: 24133 Type: Default 1000ms 256MiB

Maximum Products Delivery

Maximum Products Delivery

You are given a vehicle with a weight limit W and N products. Each product has an associated weight and a priority. Your goal is to determine the maximum number of products that the vehicle can deliver without exceeding its weight limit. Products are selected based on their priority—products with higher priority are chosen first. In case of equal priority, the product with the lower weight is chosen.

Sorting strategy:

  • Sort products in descending order of priority.
  • If priorities are equal, sort by ascending order of weight.

Input: The first line contains two integers W and N. The subsequent N lines each contain two integers representing the weight and the priority of each product.

Output: Output a single integer indicating the maximum number of products that can be delivered.

inputFormat

The input is read from stdin and has the following format:

W N
w1 p1
w2 p2
... 
wN pN

Where:

  • W is the maximum weight capacity of the vehicle.
  • N is the number of products.
  • Each of the next N lines contains two integers: the weight wi and the priority pi of the i-th product.

outputFormat

The output is a single line printed to stdout containing one integer, the maximum number of products that can be delivered without exceeding the vehicle's weight limit.

## sample
50 4
10 10
20 9
30 8
40 10
2