CF BUDDY
← Problems·

1989D · Smithing Skill

1900 · brute force, data structures, dp

Problem: You are playing a famous computer game (that just works) where you have various skills you can level up. Today, you focused on the "Smithing" skill. Your tactic is obvious: forging weapons from ingots and then melting them back to return the materials partially. For simplicity, every time you create an item, you get 11 experience point, and every time you melt an item, you also get 11 experience point.

There are nn classes of weapons you can forge and mm types of metal ingots.

You can create one weapon of the ii-th class, spending aia_i ingots of metal of the same type. Melting a weapon of the ii-th class (which you crafted earlier) returns you bib_i ingots of the type of metal it was made of.

You have cjc_j metal ingots of the jj-th type, and you know that you can craft a weapon of any class from any metal type. Each combination of a weapon class and a metal type can be used any number of times.

What is the maximum total amount of experience you can earn by crafting and melting weapons?

Input Format: The first line contains two integers nn and mm (1n,m1061 \le n, m \le 10^6) — the number of weapon classes and metal types.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1061 \le a_i \le 10^6), where aia_i is the number of ingots you need to forge one weapon of the ii-th class.

The third line contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n (0bi<ai0 \le b_i < a_i), where bib_i is the number of ingots you return by melting one weapon of the ii-th class you forged earlier.

The fourth line contains mm integers c1,c2,,cmc_1, c_2, \dots, c_m (1cj1091 \le c_j \le 10^9) — the number of ingots you have of the corresponding metal type.

Output Format: Print one integer — the maximum total experience points you can gain by repeatedly forging and melting weapons.

Note: In the first example, you can do the following:

  1. craft one weapon of the 11-st class from the 11-st type of metal, spending 99 ingots;
  2. melt that weapon, returning 88 ingots of the 11-st metal type;
  3. again, craft and melt one weapon of the 11-st class from the 11-st metal type;
  4. craft and melt one weapon of the 33-rd class from the 11-st metal type;
  5. craft and melt one weapon of the 33-rd class from the 33-rd metal type;
  6. craft and melt one weapon of the 44-th class from the 11-st metal type;
  7. craft and melt one weapon of the 55-th class from the 33-rd metal type;

Sample Cases

Case 1

Input

5 3
9 6 7 5 5
8 4 5 1 2
10 4 7

Output

12

Case 2

Input

3 4
10 20 20
0 0 0
9 10 19 20

Output

8

Case 3

Input

1 5
3
1
1000000000 1000000000 1000000000 1000000000 1000000000

Output

4999999990

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