#P5798. [SEERC 2019] Level Up

[SEERC 2019] Level Up

Description

As an MMORPG fan, Steve was very excited to hear that World of Warcraft: Classic was released. He started playing on the first day of release, and now he is only 22 levels away from reaching the maximum level. Of course, he does not have as much free time as when he first started, so he wants to reach the maximum level as quickly as possible.

To gain the first level, he needs s1s_1 experience points. Only after he has obtained that much experience can he level up, and the second level then requires another s2s_2 experience points to level up.

Steve has a quest list with nn quests. He wants to reach the maximum level by completing some of these quests. Steve needs tit_i minutes to complete quest ii, and this quest gives him xix_i experience points.

When Steve completes a quest and can level up, any experience beyond what is required to level up will carry over to the next level. After he levels up once, the experience gained from quest ii decreases to yiy_i, and the time needed to complete it also decreases to rir_i.

Note that quests Steve has completed cannot be taken again, regardless of whether he has leveled up.

Given the information of the quests in the list, help Steve find an order of completing quests such that the total time needed to reach the maximum level is minimized.

Input Format

The first line contains three integers n,s1,s2 (1n,s1,s2500)n, s_1, s_2 \ (1 \leq n, s_1, s_2 \leq 500), representing the number of quests, the experience required to gain the first level, and the experience required to gain the second level.

The next nn lines each contain four integers $x_i, t_i, y_i, r_i \ (1 \leq y_i < x_i \leq 500, 1 \leq r_i < t_i \leq 10^9)$, representing the experience gained from quest ii, the time needed to complete it, the experience gained after leveling up once, and the time needed after leveling up once.

Output Format

Output one integer, the minimum number of minutes Steve needs to reach the maximum level. If it is impossible to reach the maximum level no matter what, output 1-1.

2 100 100
100 100 10 10
101 11 100 10
110
4 20 20
40 1000 20 20
6 6 5 5
10 10 1 1
10 10 1 1
40
2 20 5
10 10 5 5
10 10 5 5
-1

Hint

Translated by ChatGPT 5