G - Cut and Reorder Editorial /

Time Limit: 2.8 sec / Memory Limit: 512 MB

配点 : 575575

問題文

長さ NN の数列 A=(A1,A2,,AN),B=(B1,B2,,BN)A=(A _ 1,A _ 2,\ldots,A _ N),B=(B _ 1,B _ 2,\ldots,B _ N) が与えられます。

あなたは、数列 AA に対して次の 22 種類の操作を好きな順番で好きな回数行うことができます。

  • AA を好きな位置で分割し、分割された列を自由に並べ替える。分割した位置 11 つにつきコストが CC かかる。 厳密には、(X1)C(X-1)C のコストをかけて長さ X+1X+1 の列 (i0,i1,i2,,iX) (0=i0<i1<i2<<iX=N)(i _ 0,i _ 1,i _ 2,\ldots,i _ X)\ (0=i _ 0\lt i _ 1\lt i _ 2\lt\cdots\lt i _ X=N)(1,2,,X)(1,2,\ldots,X) の順列 pp を自由にとり、(Aipj1+1,Aipj1+2,,Aipj)(A _ {i _ {p _ j-1}+1},A _ {i _ {p _ j-1}+2},\ldots,A _ {i _ {p _ j}})jj の昇順に連結したものを新しい AA とする。
  • 整数 kkAA の好きな要素を 11 つ選び、選んだ要素の値に kk を加える。コストが k|k| かかる。

操作をすべて終えたときに AABB が等しくなるように操作を行うとき、必要なコストの合計の最小値を求めてください。

制約

  • 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)
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN CC
A1A _ 1 A2A _ 2 \ldots ANA _ N
B1B _ 1 B2B _ 2 \ldots BNB _ N

出力

答えを出力せよ。


入力例 1Copy

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

出力例 1Copy

Copy
12

例えば、次のように操作をすることで AABB を等しくすることができます。

  • A2A _ 222 を加える。コストが 22 かかり、A=(3,3,4,1,5)A=(3,3,4,1,5) となる。
  • A4A _ 411 を加える。コストが 11 かかり、A=(3,3,4,2,5)A=(3,3,4,2,5) となる。
  • A3A _ 333 を加える。コストが 33 かかり、A=(3,3,7,2,5)A=(3,3,7,2,5) となる。
  • AA(3,3)(3,3)(7,2,5)(7,2,5) に分割し、順番を入れ替える。コストが 11 かかり、A=(7,2,5,3,3)A=(7,2,5,3,3) となる。
  • A3A _ 311 を加える。コストが 11 かかり、A=(7,2,6,3,3)A=(7,2,6,3,3) となる。
  • A4A _ 422 を加える。コストが 22 かかり、A=(7,2,6,5,3)A=(7,2,6,5,3) となる。
  • A1A _ 122 を加える。コストが 22 かかり、A=(9,2,6,5,3)A=(9,2,6,5,3) となる。

かかるコストの合計は 2+1+3+1+1+2+2=122+1+3+1+1+2+2=12 となります。

コストの合計を 1111 以下にして AABB を等しくすることはできないため、1212 と出力してください。


入力例 2Copy

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

出力例 2Copy

Copy
15

例えば、次のように操作をすることで AABB を等しくすることができます。

  • A1A _ 166 を加える。コストが 66 かかり、A=(9,1,4,1,5)A=(9,1,4,1,5) となる。
  • A2A _ 211 を加える。コストが 11 かかり、A=(9,2,4,1,5)A=(9,2,4,1,5) となる。
  • A3A _ 322 を加える。コストが 22 かかり、A=(9,2,6,1,5)A=(9,2,6,1,5) となる。
  • A4A _ 444 を加える。コストが 44 かかり、A=(9,2,6,5,5)A=(9,2,6,5,5) となる。
  • A5A _ 52-2 を加える。コストが 22 かかり、A=(9,2,6,5,3)A=(9,2,6,5,3) となる。

かかるコストの合計は 1515 となります。

コストの合計を 1414 以下にして AABB を等しくすることはできないため、1515 と出力してください。


入力例 3Copy

Copy
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

出力例 3Copy

Copy
4370668608634071

入力や答えが 32bit32\operatorname{bit} 整数に収まらない場合があります。

Score : 575575 points

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, (i0,i1,i2,,iX) (0=i0<i1<i2<<iX=N)(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 (Aipj1+1,Aipj1+2,,Aipj)(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 1Copy

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

Sample Output 1Copy

Copy
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 2Copy

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

Sample Output 2Copy

Copy
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 3Copy

Copy
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 3Copy

Copy
4370668608634071

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



2025-04-28 (Mon)
19:21:53 +00:00