#P3173. [HAOI2009] 巧克力

[HAOI2009] 巧克力

Description

There is a rectangular chocolate bar of size n×mn \times m, to be cut into n×mn \times m pieces. There are n1n-1 horizontal lines and m1m-1 vertical lines on the bar. Each time, you may cut along one of these horizontal or vertical lines. Regardless of the cut length, the costs of cutting along the horizontal lines in order are y1,y2,,yn1y_1,y_2,\cdots,y_{n-1}, and along the vertical lines in order are x1,x2,,xm1x_1,x_2,\cdots,x_{m-1}.

For the 6×46 \times 4 chocolate shown below, suppose we first cut along the three horizontal lines, requiring 33 cuts, producing 44 chocolate strips. Then we cut these 44 strips along the vertical lines, each requiring 55 cuts. 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, the simple method above is not necessarily optimal. How should we cut this chocolate bar to minimize the total cost?

Input Format

The first line contains two integers nn and mm.

The next n1n-1 lines each contain one integer, representing y1,y2,,yn1y_1,y_2,\cdots,y_{n-1}.

The next m1m-1 lines each contain one integer, representing x1,x2,,xm1x_1,x_2,\cdots,x_{m-1}.

Output Format

Output a single integer, the minimum cost to cut the chocolate.

6 4
2
1
3
1
4
4
1
2
42

Hint

For 30%30\% of the testdata, n100,m100n \leq 100,m \leq 100.

For 100%100\% of the testdata, n10000,m10000n \leq 10000,m \leq 10000.

Translated by ChatGPT 5