#abc290h. Ex - Bow Meow Optimization

Ex - Bow Meow Optimization

Score : 600600 points

问题描述

设有 NN 只编号为 11NN 的狗和 MM 只编号为 11MM 的猫。你需要将这 (N+M)(N+M) 只动物从左至右排列成一行。这种排列方式将会导致每只动物的 不满值 如下:

  • ii 的不满值为 Ai×xyA_i\times|x-y|, 其中 xxyy 分别表示该狗左右两侧的猫的数量。
  • ii 的不满值为 Bi×xyB_i\times|x-y|, 其中 xxyy 分别表示该猫左右两侧的狗的数量。

请找出这些动物不满值之和的最小可能值。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN dogs numbered 11 through NN and MM cats numbered 11 through MM. You will arrange the (N+M)(N+M) animals in a line from left to right. An arrangement causes each animal's frustration as follows:

  • The frustration of dog ii is Ai×xyA_i\times|x-y|, where xx and yy are the numbers of cats to the left and right of that dog, respectively.
  • The frustration of cat ii is Bi×xyB_i\times|x-y|, where xx and yy are the numbers of dogs to the left and right of that cat, respectively.

Find the minimum possible sum of the frustrations.

Constraints

  • 1N,M3001\leq N,M \leq 300
  • 1Ai,Bi1091\leq A_i,B_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM

A1A_1 A2A_2 \ldots ANA_N

B1B_1 B2B_2 \ldots BMB_M

Output

Print the answer as an integer.

Sample Input 1

2 2
1 3
2 4

Sample Output 1

6

Consider the following arrangement: dog 11, cat 22, dog 22, cat 11, from left to right. Then,

  • dog 11's frustration is 1×02=21\times|0-2|=2;
  • dog 22's frustration is 3×11=03\times|1-1|=0;
  • cat 11's frustration is 2×20=42\times|2-0|=4; and
  • cat 22's frustration is 4×11=04\times|1-1|=0,

so the sum of the frustrations is 66. Rearranging the animals does not make the sum less than 66, so the answer is 66.

Sample Input 2

1 2
100
100 290

Sample Output 2

390

Sample Input 3

5 7
522 575 426 445 772
81 447 629 497 202 775 325

Sample Output 3

13354

update @ 2024/3/10 12:07:55