Codeforces Round 230 (Div. 2)
D
Tower of Hanoi
Codeforces
未评级
原题
← CE →
翻译中…

The Tower of Hanoi is a well-known mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

翻译中…

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

翻译中…
  1. Only one disk can be moved at a time.
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
  3. No disk may be placed on top of a smaller disk.
翻译中…

With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is $2^{*n*} - 1$, where $*n*$ is the number of disks. (c) Wikipedia.

翻译中…

SmallY's puzzle is very similar to the famous Tower of Hanoi. In the Tower of Hanoi puzzle you need to solve a puzzle in minimum number of moves, in SmallY's puzzle each move costs some money and you need to solve the same puzzle but for minimal cost. At the beginning of SmallY's puzzle all $*n*$ disks are on the first rod. Moving a disk from rod $*i*$ to rod $*j*$ $(1 ≤ *i*, *j* ≤ 3)$ costs $*t*_{*ij*}$ units of money. The goal of the puzzle is to move all the disks to the third rod.

翻译中…

In the problem you are given matrix $*t*$ and an integer $*n*$. You need to count the minimal cost of solving SmallY's puzzle, consisting of $*n*$ disks.

翻译中…

Input

翻译中…

Each of the first three lines contains three integers — matrix $*t*$. The $*j*$-th integer in the $*i*$-th line is $*t*_{*ij*}$ ($1 ≤ *t*_{*ij*} ≤ 10000; *i* ≠ *j*$). The following line contains a single integer $*n*$ $(1 ≤ *n* ≤ 40)$ — the number of disks.

翻译中…

It is guaranteed that for all $*i*$ $(1 ≤ *i* ≤ 3)$, $*t*_{*ii*} = 0$.

翻译中…

Output

翻译中…

Print a single integer — the minimum cost of solving SmallY's puzzle.

翻译中…

样例

Input #1
0 1 1
1 0 1
1 1 0
3
Output #1
7
InputCopy OutputCopy
Input #2
0 2 2
1 0 100
1 2 0
3
Output #2
19
InputCopy OutputCopy
Input #3
0 2 1
1 0 100
1 2 0
5
Output #3
87
InputCopy OutputCopy
题目信息
题号
393D
时间限制
1 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。