#P5948. [POI 2003 R1] Chocolate

[POI 2003 R1] Chocolate

Problem Description

There is an n×mn\times m rectangular chocolate bar, and you want to cut it into n×mn\times m small pieces.

On the chocolate there are n1n-1 horizontal lines and m1m-1 vertical lines. Each time, you may cut the chocolate along one of these horizontal or vertical lines. No matter how long the cut is, the costs of cutting along the horizontal lines (one time per line) are y1,y2,,yn1y_1,y_2,\dots,y_{n-1} in order, and the costs of cutting along the vertical lines are x1,x2,,xm1x_1,x_2,\dots,x_{m-1} in order.

For example, for the 6×46\times 4 chocolate in the figure below:

If we first cut along three horizontal lines, it takes 33 cuts and we get 44 strips of chocolate. Then we cut these 44 strips along the vertical lines. Each strip needs 55 cuts, so the total cost is y1+y2+y3+4×(x1+x2+x3+x4+x5)y_1+y_2+y_3+4\times (x_1+x_2+x_3+x_4+x_5).

Of course, this simple method may not be optimal. How should you cut the chocolate so that the total cost is minimal?

Input Format

The first line contains two integers nn and mm.

The next n1n-1 lines each contain one integer, representing x1,x2,,xn1x_1,x_2,\dots,x_{n-1}.

The next m1m-1 lines each contain one integer, representing y1,y2,,ym1y_1,y_2,\dots,y_{m-1}.

Output Format

Output one integer, the minimum total cost to cut the chocolate.

6 4
2
1
3
1
4
4
1
2
42

Hint

Constraints: for 100%100\% of the testdata, 1n100001\le n\le 10000 and 1m100001\le m\le 10000.

Translated by ChatGPT 5