Sequential Manufacturing

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

A certain manufacturing process consists of NN different machines, through which raw material pass in order until it becomes the manufactured goods. The ii'th machine takes T_iT\_i seconds to process a good when input.

A good moves automatically through the system once input into the first system. To give the system some tolerance with regards to timing, each machine ii has an input area where K_iK\_i items waiting to be processed by that machine can be stored. This means that when machine ii finishes processing a good, it is either passed on immediately to machine i+1i + 1 if it is free, and otherwise placed in its input area until that machine becomes free and can process the good. If a machine finishes a good, and the next machine is currently being used, and its input area is full, the system shuts down. If a machine and the one after it finish processing an item at the same time, that item does not need to be put in the input area of the second machine first.

You just got a huge order of goods, and now wonder -- how quickly can you finish manufacturing these goods, without the system shutting down? You have an infinite supply of raw materials, and can choose at which times you put in items in machine 11.

입력

The first line contains the two integers NN (1N1,0001 \le N \le 1\\,000), the number of machines, and PP (1P1091 \le P \le 10^9), the number of items that should be produced.

The next line contains the processing times T_1T\_1, \dots, T_NT\_N (1T_i1091 \le T\_i \le 10^9), which are all integers.

The next line contains the sizes of the input areas K_2K\_2, \dots, K_NK\_N (1K_i1091 \le K\_i \le 10^9), which are all integers. Note that the first machine does not have an input area.

출력

Output the number of seconds it takes to manufacture all the requested items. Manufacturing is considered complete when the last item has been processed by the last machine.