#P5585. 「SWTR-1」Doing Homework

「SWTR-1」Doing Homework

Background

Little A\mathrm{A} has a lot of homework to do every day.

Problem Description

Every day, Little A\mathrm{A} must do at least ww tons of homework. If he fails to reach the goal, he will be punished by Little S\mathrm{S} and die on the spot.

Little A\mathrm{A} has xx energy points. Each time he does homework, Little A\mathrm{A}’s energy decreases, and this is irreversible. Little A\mathrm{A}’s energy cannot drop below 00.

Now, there are nn types of homework for Little A\mathrm{A} to choose from.

Each type of homework has the following attributes:

xix_i: energy cost, i.e., doing one copy of this type of homework requires xix_i energy.

wiw_i: weight, i.e., one copy of this type of homework weighs wiw_i tons.

tit_i: deadline, i.e., after tit_i days from today, this homework can no longer be done.

There are infinitely many copies of each type of homework.

Since he has too much homework to ever finish, please arrange a homework plan for him to maximize the number of days he can survive. When the number of surviving days is maximized, maximize his remaining energy.

Input Format

The first line contains two positive integers x,wx, w, representing Little A\mathrm{A}’s energy and the daily goal.

The next line contains one positive integer nn, indicating the number of types of homework.

The next nn lines each contain three integers xi,wi,tix_i, w_i, t_i.

Output Format

Output two numbers separated by a space, representing the maximum number of days Little A\mathrm{A} can survive, and his remaining energy.

30 4
3
5 3 8
3 2 2
8 4 4
4 2
100 3
2
3 2 8
2 1 5
8 57

Hint


Sample Explanation

On day 11, Little A\mathrm{A} chooses to do 22 copies of the second type of homework. The weight is 22=42 * 2 = 4, and the remaining energy is 3023=2430 - 2 * 3 = 24.

On day 22, Little A\mathrm{A} chooses to do 22 copies of the second type of homework. The weight is 22=42 * 2 = 4, and the remaining energy is 2423=1824 - 2 * 3 = 18.

At this point, he can no longer do the second type of homework (t2=2)(t_2 = 2).

On day 33, Little A\mathrm{A} chooses to do 11 copy of the third type of homework. The weight is 44, and the remaining energy is 188=1018 - 8 = 10.

On day 44, Little A\mathrm{A} chooses to do 11 copy of the third type of homework. The weight is 44, and the remaining energy is 108=210 - 8 = 2.

At this point, he can no longer do the third type of homework (t3=4)(t_3 = 4).

Little A\mathrm{A} has no energy left to do any other homework, so he can survive at most 44 days, with 22 energy remaining.

It can be proven that no better plan than this one can be found.


Constraints


For 25%25\% of the testdata with n5000n \leq 5000, the time limit is 1 s1\ \mathrm{s} and the memory limit is 256 MB256\ \mathrm{MB}.

For the remaining test points, the time limit is 400 ms400\ \mathrm{ms} and the memory limit is 8 MB8\ \mathrm{MB}.

Translated by ChatGPT 5