U, Our Star!
InterviewTime limit1sMemory limit1024 MB
Given products with limited counts and prices, find the smallest payment that maximizes points, where points are floor(payment / X) times Y, capped at D.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Math
- Solved
- No attempts yet
Problem
The Seoul City University cafe sells various Iruemae goods that can only be bought with U, Our Star! points.
The cafe runs the following event.
- For every won spent at the cafe, you receive of U, Our Star! points.
- The maximum number of U, Our Star! points you can receive is .
Alimi loves cute Iruemae goods and wants to collect as many U, Our Star! points as possible during this event. Alimi has enough money to buy any products as many times as desired. However, Alimi dislikes overspending, so Alimi wants to pay the smallest amount among the payments that maximize the points.
Given the products as pairs of available quantity and price, find the minimum payment that maximizes the U, Our Star! points received.
Input
The first line contains three positive integers , , and .
- is a positive multiple of , and .
The second line contains the number of products . ()
Each of the next lines contains the available quantity and the price of a product, separated by a space. (, )
The total available quantity of all products is between 1 and 100, inclusive.
Output
Print the minimum payment that maximizes the U, Our Star! points received.
If you cannot receive any U, Our Star! points even after buying all products, print .