PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000458. Phép thuật cứu thế giới (Magic Will Save the World)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 nn monsters threatening the world. Monster ii has strength sis_i. Vika can defeat monsters using either water magic or fire magic.

She starts with no mana. Each second adds ww units of water mana and ff units of fire mana to her reserves. Unused mana can be saved. Defeating monster ii requires a single spell consuming at least sis_i 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 w,fw,f.

The second line contains an integer nn.

The third line contains nn integers s1,s2,…,sns_1,s_2,\ldots,s_n.

Output

Print the minimum time in whole seconds.

Subtasks

  • Subtask 1 (20 points): 1≤w,f≤1091\le w,f\le10^9; 1≤n≤181\le n\le18; 1≤i≤n1\le i\le n; 1≤si≤100001\le s_i\le10000.
  • Subtask 2 (30 points): 1≤w,f≤1091\le w,f\le10^9; 1≤n≤1001\le n\le100; 1≤i≤n1\le i\le n; 1≤si≤1001\le s_i\le100.
  • Subtask 3 (50 points): 1≤w,f≤1091\le w,f\le10^9; 1≤n≤1001\le n\le100; 1≤i≤n1\le i\le n; 1≤si≤100001\le s_i\le10000.

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.