No.2656 XOR Slimes
Problem Statement
There are $N$ slimes on a number line extending left and right.
The $i$-th slime from the left $(i=1,2, \cdots ,N)$
holds an integer $A_i$ and is at position $X_i$.
The slimes can perform the following merge action any number of times, possibly $0$ times.
- First, choose $2$ slimes to merge
- Let their positions be $X_P,X_Q$ and the integers they hold be $A_P,A_Q$
- Next, freely choose a real number $x$
- The $2$ slimes move to position $x$ and merge into $1$ slime
- The movement incurs a movement cost of $|X_P-x|+|X_Q-x|$
- The integer held by the merged slime is the XOR of $A_P$ and $A_Q$
Let $C$ be the total movement cost of all merge actions.
Let $S$ be the sum of the integers held by the slimes remaining at the end.
The slimes acted so that $C+S$ is minimized.
Find the value of $C+S$.
Constraints
- All inputs are integers
- $2 \le N \le 5000$
- $1 \le X_1 \lt X_2 \lt \cdots \lt X_N \le 10^9$
- $1 \le A_i \lt 2^{30}$
Input
The input is given from standard input in the following format.$N$ $X_1$ $X_2$ $\cdots$ $X_N$ $A_1$ $A_2$ $\cdots$ $A_N$
Output
Output the value of $C+S$ as an integer, followed by a newline.
It can be proved that the value of $C+S$ is an integer under the constraints of this problem.
Sample
Sample 1
Input
3 2 4 5 2 1 6
Output
8
If the $1$-st and $3$-rd slimes from the left merge at $x=3.5$,
the integer held by the merged slime is $2$ XOR $6 = 4$.
The movement cost in this case is $|2-3.5|+|5-3.5|=3$.
If the merge actions end here, the total movement cost is
$C=3$ and the sum of the integers is $S=5$, so $C+S=8$.
No sequence of merge actions makes $C+S$ less than $8$, so the answer is $8$.
Sample 2
Input
4 1 4 5 7 1 1 1 1
Output
3
Sample 3
Input
4 10 20 30 40 1 1 1 1
Output
4
Sample 4
Input
3 3 7 15 5 3 6
Output
12
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
hamamu
てんぷら