CF BUDDY
← Problems·

1862F · Magic Will Save the World

1800 · binary search, bitmasks, brute force

Problem: A portal of dark forces has opened at the border of worlds, and now the whole world is under a terrible threat. To close the portal and save the world, you need to defeat nn monsters that emerge from the portal one after another.

Only the sorceress Vika can handle this. She possesses two magical powers — water magic and fire magic. In one second, Vika can generate ww units of water mana and ff units of fire mana. She will need mana to cast spells. Initially Vika have 00 units of water mana and 00 units of fire mana.

Each of the nn monsters that emerge from the portal has its own strength, expressed as a positive integer. To defeat the ii-th monster with strength sis_i, Vika needs to cast a water spell or a fire spell of at least the same strength. In other words, Vika can spend at least sis_i units of water mana on a water spell, or at least sis_i units of fire mana on a fire spell.

Vika can create and cast spells instantly. Vika can cast an unlimited number of spells every second as long she has enough mana for that.

The sorceress wants to save the world as quickly as possible, so tell her how much time she will need.

Input Format: Each test consists of several test cases. The first line of each test contains a single integer tt (1t1001 \le t \le 100) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains two integers ww, ff (1w,f1091 \le w, f \le 10^9) — the amount of water and fire mana that Vika can generate per second.

The second line of each test case contains a single integer nn (1n1001 \le n \le 100) — the number of monsters.

The third line of each test case contains nn integers s1,s2,s3,,sns_1, s_2, s_3, \dots, s_n (1si1041 \le s_i \le 10^4) — the strengths of the monsters.

It is guaranteed that the sum of nn over all test cases does not exceed 100100.

Output Format: For each test case, output a single integer — the minimum time in seconds that Vika will need to defeat all the monsters.

Note: In the first sample, after the first second, Vika can spend 22 units of fire mana to kill the first monster. Then she has 22 units of water mana and 11 unit of fire mana. After the third second, she will have 66 units of water mana and 77 units of fire mana at her disposal. This will be enough to immediately kill the second and third monsters.

Sample Cases

Case 1

Input

4
2 3
3
2 6 7
37 58
1
93
190 90
2
23 97
13 4
4
10 10 2 45

Output

3
2
1
5

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback