#abc328g. G - Cut and Reorder

G - Cut and Reorder

Score : 575575 points

问题描述

给定两个长度为 NN 的序列:A=(A1,A2,,AN)A=(A_1,A_2,\ldots,A_N)B=(B1,B2,,BN)B=(B_1,B_2,\ldots,B_N)

你可以对序列 AA 进行任意次数、任意顺序的以下两种操作:

  • AA 在任意位置分割并自由重排分割后的序列。每次分割操作会产生一个代价 CC。更形式化地表述,以代价 (X1)C(X-1)C 来进行操作,取一个长度为 X+1X+1 的子序列索引集合 $(i_0,i_1,i_2,\ldots,i_X)\ (0=i_0<i_1<i_2<\cdots<i_X=N)$ 以及该集合的一个排列 pp(排列自 (1,2,,X)(1,2,\ldots,X)),然后按照 jj 的升序,将 AA 替换为 $(A_{i_{p_j-1}+1},A_{i_{p_j-1}+2},\ldots,A_{i_{p_j}})$ 的串联。
  • 选择一个整数 kk 以及 AA 中的任意一个元素,并将所选元素的值增加 kk,这个操作的代价为 k|k|

通过执行这些操作,找到使 AABB 相等所需的最小总代价。

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

Problem Statement

You are given two sequences of length NN: A=(A1,A2,,AN)A=(A_1,A_2,\ldots,A_N) and B=(B1,B2,,BN)B=(B_1,B_2,\ldots,B_N).

You can perform the following two types of operations on the sequence AA any number of times in any order.

  • Split AA at any positions and freely rearrange the split sequences. Each split position incurs a cost of CC. More formally, at a cost of (X1)C(X-1)C, take a sequence of length X+1X+1, $(i_0,i_1,i_2,\ldots,i_X)\ (0=i_0\lt i_1\lt i_2\lt\cdots\lt i_X=N)$, and a permutation pp of (1,2,,X)(1,2,\ldots,X), and replace AA with the concatenation of $(A_{i_{p_j-1}+1},A_{i_{p_j-1}+2},\ldots,A_{i_{p_j}})$ in ascending order of jj.
  • Choose an integer kk and any element of AA, and add kk to the value of the chosen element, for a cost of k|k|.

Find the minimum total cost required to make AA and BB equal by performing the operations.

Constraints

  • 1N221\leq N\leq22
  • 1C10151\leq C\leq10^{15}
  • 1Ai1015 (1iN)1\leq A_i\leq10^{15}\ (1\leq i\leq N)
  • 1Bi1015 (1iN)1\leq B_i\leq10^{15}\ (1\leq i\leq N)
  • All input values are integers.

Input

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

NN CC

A1A_1 A2A_2 \ldots ANA_N

B1B_1 B2B_2 \ldots BNB_N

Output

Print the answer.

Sample Input 1

5 1
3 1 4 1 5
9 2 6 5 3

Sample Output 1

12

For example, you can make AA and BB equal by performing the following operations.

  • Add 22 to A2A_2. It costs 22, and AA becomes (3,3,4,1,5)(3,3,4,1,5).
  • Add 11 to A4A_4. It costs 11, and AA becomes (3,3,4,2,5)(3,3,4,2,5).
  • Add 33 to A3A_3. It costs 33, and AA becomes (3,3,7,2,5)(3,3,7,2,5).
  • Split AA into (3,3)(3,3) and (7,2,5)(7,2,5), and swap their order. It costs 11, and AA becomes (7,2,5,3,3)(7,2,5,3,3).
  • Add 11 to A3A_3. It costs 11, and AA becomes (7,2,6,3,3)(7,2,6,3,3).
  • Add 22 to A4A_4. It costs 22, and AA becomes (7,2,6,5,3)(7,2,6,5,3).
  • Add 22 to A1A_1. It costs 22, and AA becomes (9,2,6,5,3)(9,2,6,5,3).

The total cost incurred is 2+1+3+1+1+2+2=122+1+3+1+1+2+2=12.

It is impossible to make AA and BB equal with a total cost of 1111 or less, so print 1212.

Sample Input 2

5 1000000000
3 1 4 1 5
9 2 6 5 3

Sample Output 2

15

For example, you can make AA and BB equal by performing the following operations.

  • Add 66 to A1A_1. It costs 66, and AA becomes (9,1,4,1,5)(9,1,4,1,5).
  • Add 11 to A2A_2. It costs 11, and AA becomes (9,2,4,1,5)(9,2,4,1,5).
  • Add 22 to A3A_3. It costs 22, and AA becomes (9,2,6,1,5)(9,2,6,1,5).
  • Add 44 to A4A_4. It costs 44, and AA becomes (9,2,6,5,5)(9,2,6,5,5).
  • Add 2-2 to A5A_5. It costs 22, and AA becomes (9,2,6,5,3)(9,2,6,5,3).

The total cost incurred is 1515.

It is impossible to make AA and BB equal with a total cost of 1414 or less, so print 1515.

Sample Input 3

22 467772225675200
814424018890229 837987908732596 281175505732576 405797525366223 319378664987871 305374284356649 519144936694626 316916938328237 590332737480143 506785561790072 945769796193819 365498597798550 5386616044591 672368930784037 478017750715806 340276460237787 176509793332130 2734777402752 677509027289850 250325127275409 260270543315523 103584313625431
720386673780641 77160494100361 540947273460639 255177791002759 969333325196025 477751866935037 369600749728569 466236682780196 343161112138696 541310338013515 42740499599240 165778332156355 618106559852784 16582487395877 591851763813728 221861304303645 982850624742022 728669467505250 337968530842725 746724490610504 61587851254728 451153536869240

Sample Output 3

4370668608634071

Note that the input and the answer may not fit into a 32bit32\operatorname{bit} integer.

update @ 2024/3/10 01:53:41