#P7747. [COCI 2011/2012 #3] TRAKA

[COCI 2011/2012 #3] TRAKA

Description

There are nn workers in Mirko’s factory. They manufacture cars on a conveyor belt in an assembly-line manner. The workers are numbered from left to right as 1n1 \sim n, and worker 11 is Mirko. The production of a car starts with worker 11 (Mirko). After he finishes all his work, worker 22 takes over. Then worker 33 takes over worker 22’s work, and so on. When worker nn finishes his work, the car is completed.

Mirko and his workers must produce mm cars, and they must be produced in order from 1m1 \sim m. For worker ii, the time needed to complete his part is tit_i. For car jj, its assembly complexity is fjf_j. The time worker ii needs to finish his work on car jj is tifjt_i \cdot f_j.

According to company policy, after a worker finishes his work, he must immediately pass the work to the next worker without any delay. Therefore, at that moment, the next worker must not be working on another car. To satisfy this condition, Mirko must wait for a suitable moment to start making a new car. To improve efficiency, he will wait the minimum amount of time until he is sure that all conditions can be satisfied.

Write a program that, given the time needed for each worker to complete his work and the assembly complexity of each car, computes the total time required to produce all cars.

Input Format

The input contains a total of n+m+2n + m + 2 lines.

The first line contains two integers n,mn, m, representing the number of workers and the number of cars to be produced.
The next nn lines each contain one integer tit_i, denoting the time needed for worker ii to complete his work.
The next mm lines each contain one integer fjf_j, denoting the assembly complexity of car jj.

Output Format

Output a single line containing one integer, representing the total time required to produce all cars.

3 3
2
1
1
2
1
1
11
3 3
2
3
3
2
1
2
29
4 5
3
2
2
2
3
1
2
1
2
55

Hint

[Sample 1 Explanation]

For sample 11, after 44 units of time, worker 11 finishes his work on the first car. He could immediately start working on the second car, but that would violate the condition that a car must be passed to the next worker immediately after it is finished (after 77 units of time, worker 22 would finish his work on the second car, but worker 33 cannot take over because he is still working on the first car). Therefore, the second car can only start production after 55 units of time. The third car starts production after 77 units of time. The first car is completed after 88 units of time, the second car after 99 units of time, and the third car after 1111 units of time. Therefore, the total time is 1111.

[Constraints]

For 40%40\% of the testdata, n,m1000n, m \leqslant 1000.
For all testdata, 1n,m1051 \leqslant n, m \leqslant 10^5, and 1ti,fj1041 \leqslant t_i, f_j \leqslant 10^4.

[Source]

This problem comes from COCI 2011-2012 CONTEST 3 T6 TRAKA, using the original testdata configuration, with a full score of 160160 points.

Translated and organized by Eason_AC.

Translated by ChatGPT 5