Under construction

Still being built. Things may change

Sequential Manufacturing

Time limit1sMemory limit1024 MB

Summary
Given machine times T_i and input buffer sizes K_i, find the minimum total time to finish P goods without the system shutting down.
Level

Medium7 of 10

Topics
Greedy, Math
Solved
No attempts yet

Problem

A manufacturing process consists of NN different machines. Raw material passes through the machines in order until it becomes a finished good. Machine ii takes TiT_i seconds to process a good once it is input.

A good moves through the system automatically after it enters the first machine. To give the system some slack, each machine ii has an input area that holds up to KiK_i goods waiting to be processed by that machine. When machine ii finishes a good, the good moves on to machine i+1i + 1 right away if that machine is free. Otherwise it waits in the input area of machine i+1i + 1 until the machine becomes free. If machine ii finishes a good while machine i+1i + 1 is busy and its input area is full, the system shuts down. If machines ii and i+1i + 1 finish goods at the same moment, the good from machine ii does not need to go through the input area of machine i+1i + 1 first.

You just received a huge order. You have an infinite supply of raw material, and you can choose the times at which goods enter machine 1. How quickly can you finish all of these goods without the system shutting down?

Input

The first line contains two integers NN (1≤N≤1 0001 \le N \le 1\,000), the number of machines, and PP (1≤P≤1091 \le P \le 10^9), the number of goods to produce.

The next line contains the processing times T1,…,TNT_1, \dots, T_N (1≤Ti≤1091 \le T_i \le 10^9), all integers.

The next line contains the sizes of the input areas K2,…,KNK_2, \dots, K_N (1≤Ki≤1091 \le K_i \le 10^9), all integers. The first machine has no input area.

Output

Output the number of seconds needed to produce all PP goods. Manufacturing is complete when the last good is processed by the last machine.

Examples1

  1. Example 1

    Input
    2 3
    1 5
    100
    
    Expected output
    16