103286: [Atcoder]ABC328 G - Cut and Reorder
Description
Score : $575$ points
Problem Statement
You are given two sequences of length $N$: $A=(A_1,A_2,\ldots,A_N)$ and $B=(B_1,B_2,\ldots,B_N)$.
You can perform the following two types of operations on the sequence $A$ any number of times in any order.
- Split $A$ at any positions and freely rearrange the split sequences. Each split position incurs a cost of $C$. More formally, at a cost of $(X-1)C$, take a sequence of length $X+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 $p$ of $(1,2,\ldots,X)$, and replace $A$ 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 $j$.
- Choose an integer $k$ and any element of $A$, and add $k$ to the value of the chosen element, for a cost of $|k|$.
Find the minimum total cost required to make $A$ and $B$ equal by performing the operations.
Constraints
- $1\leq N\leq22$
- $1\leq C\leq10^{15}$
- $1\leq A_i\leq10^{15}\ (1\leq i\leq N)$
- $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:
$N$ $C$ $A_1$ $A_2$ $\ldots$ $A_N$ $B_1$ $B_2$ $\ldots$ $B_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 $A$ and $B$ equal by performing the following operations.
- Add $2$ to $A_2$. It costs $2$, and $A$ becomes $(3,3,4,1,5)$.
- Add $1$ to $A_4$. It costs $1$, and $A$ becomes $(3,3,4,2,5)$.
- Add $3$ to $A_3$. It costs $3$, and $A$ becomes $(3,3,7,2,5)$.
- Split $A$ into $(3,3)$ and $(7,2,5)$, and swap their order. It costs $1$, and $A$ becomes $(7,2,5,3,3)$.
- Add $1$ to $A_3$. It costs $1$, and $A$ becomes $(7,2,6,3,3)$.
- Add $2$ to $A_4$. It costs $2$, and $A$ becomes $(7,2,6,5,3)$.
- Add $2$ to $A_1$. It costs $2$, and $A$ becomes $(9,2,6,5,3)$.
The total cost incurred is $2+1+3+1+1+2+2=12$.
It is impossible to make $A$ and $B$ equal with a total cost of $11$ or less, so print $12$.
Sample Input 2
5 1000000000 3 1 4 1 5 9 2 6 5 3
Sample Output 2
15
For example, you can make $A$ and $B$ equal by performing the following operations.
- Add $6$ to $A_1$. It costs $6$, and $A$ becomes $(9,1,4,1,5)$.
- Add $1$ to $A_2$. It costs $1$, and $A$ becomes $(9,2,4,1,5)$.
- Add $2$ to $A_3$. It costs $2$, and $A$ becomes $(9,2,6,1,5)$.
- Add $4$ to $A_4$. It costs $4$, and $A$ becomes $(9,2,6,5,5)$.
- Add $-2$ to $A_5$. It costs $2$, and $A$ becomes $(9,2,6,5,3)$.
The total cost incurred is $15$.
It is impossible to make $A$ and $B$ equal with a total cost of $14$ or less, so print $15$.
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 $32\operatorname{bit}$ integer.
Input
Output
问题描述
你被给定两个长度为$N$的序列$A=(A_1,A_2,\ldots,A_N)$和$B=(B_1,B_2,\ldots,B_N)$。
你可以按任意顺序任意次数对序列$A$执行以下两种操作。
- 在任意位置将$A$分割,并自由重新排列分割的序列。每个分割位置的成本为$C$。 更正式地,在$(X-1)C$的成本下,取一个长度为$X+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)$和一个$(1,2,\ldots,X)$的排列$p$,将$A$替换为按$j$的升序连接的$(A_{i_{p_j-1}+1},A_{i_{p_j-1}+2},\ldots,A_{i_{p_j}})$。
- 选择一个整数$k$和$A$中的任意元素,并将$k$加到所选元素的值上,成本为$|k|$。
通过执行操作使$A$和$B$相等所需的最小总成本是多少?
限制条件
- $1\leq N\leq22$
- $1\leq C\leq10^{15}$
- $1\leq A_i\leq10^{15}\ (1\leq i\leq N)$
- $1\leq B_i\leq10^{15}\ (1\leq i\leq N)$
- 所有输入值都是整数。
输入
输入从标准输入按以下格式给出:
$N$ $C$ $A_1$ $A_2$ $\ldots$ $A_N$ $B_1$ $B_2$ $\ldots$ $B_N$
输出
输出答案。
样例输入 1
5 1 3 1 4 1 5 9 2 6 5 3
样例输出 1
12
例如,可以通过执行以下操作使$A$和$B$相等。
- 将$2$加到$A_2$。成本为$2$,$A$变为$(3,3,4,1,5)$。
- 将$1$加到$A_4$。成本为$1$,$A$变为$(3,3,4,2,5)$。
- 将$3$加到$A_3$。成本为$3$,$A$变为$(3,3,7,2,5)$。
- 将$A$分割为$(3,3)$和$(7,2,5)$,并交换它们的顺序。成本为$1$,$A$变为$(7,2,5,3,3)$。
- 将$1$加到$A_3$。成本为$1$,$A$变为$(7,2,6,3,3)$。
- 将$2$加到$A_4$。成本为$2$,$A$变为$(7,2,6,5,3)$。
- 将$2$加到$A_1$。成本为$2$,$A$变为$(9,2,6,5,3)$。
总共发生的成本是$2+1+3+1+1+2+2=12$。
不可能在总成本为$11$或更少的情况下使$A$和$B$相等,因此输出$12$。
样例输入 2
5 1000000000 3 1 4 1 5 9 2 6 5 3
样例输出 2
15
例如,可以通过执行以下操作使$A$和$B$相等。
- 将$6$加到$A_1$。成本为$6$,$A$变为$(9,1,4,1,5)$。
- 将$1$加到$A_2$。成本为$1$,$A$变为$(9,2,4,1,5)$。
- 将$2$加到$A_3$。成本为$2$,$A$变为$(9,2,6,1,5)$。
- 将$4$加到$A_4$。成本为$4$,$A$变为$(9,2,6,5,5)$。
- 将$-2$加到$A_5$。成本为$2$,$A$变为$(9,2,6,5,3)$。
总共发生的成本是$15$。
不可能在总成本为$14$或更少的情况下使$A$和$B$相等,因此输出$15$。
样例输入 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 16