#BS0000458. Phép thuật cứu thế giới (Magic Will Save the World)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Phép thuật cứu thế giới (Magic Will Save the World)
Magic Will Save the World
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are monsters threatening the world. Monster has strength . Vika can defeat monsters using either water magic or fire magic.
She starts with no mana. Each second adds units of water mana and units of fire mana to her reserves. Unused mana can be saved. Defeating monster requires a single spell consuming at least units of one type of mana. The two mana types cannot be combined in the same spell.
Spells take no time to cast, and any number may be cast at a given time as long as the corresponding mana reserves suffice. Find the minimum number of seconds needed to defeat every monster.
Input
The first line contains two integers .
The second line contains an integer .
The third line contains integers .
Output
Print the minimum time in whole seconds.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input 1
2 3
3
2 6 7
Output 1
3
Explanation 1
At time 3 the water and fire reserves are 6 and 9. Use water for strength 6, and fire for strengths 2 and 7. At time 2 the combined reserve is only 10, below 15.
Input 2
37 58
1
93
Output 2
2
Explanation 2
Neither reserve reaches 93 after one second. After two seconds the fire reserve is 116, which suffices.
Input 3
190 90
2
23 97
Output 3
1
Explanation 3
After one second 190 water mana suffices for both monsters, whose strengths sum to 120. No mana is available at time zero.
Input 4
13 4
4
10 10 2 45
Output 4
5
Explanation 4
At time 5, assign strengths 45 and 10 to water, and 10 and 2 to fire. At time 4, water has 52 and fire has 16. Strength 45 must use water; the remaining water reserve of 7 cannot cover either strength-10 monster, while fire cannot cover both.