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:   Only one disk can be moved at a time.  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.  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.


-----Examples-----
Input
0 1 1
1 0 1
1 1 0
3

Output
7

Input
0 2 2
1 0 100
1 2 0
3

Output
19

Input
0 2 1
1 0 100
1 2 0
5

Output
87

Difficulty: HARD
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def main():
	dp = [[[0 for _ in range(3)] for _ in range(3)] for _ in range(43)]
	matrix = [[0 for _ in range(3)] for _ in range(3)]
	for i in range(3):
		matrix[i] = get_array()
	n = int(input())
	for i in range(1, n + 1):
		for frm in range(3):
			for to in range(3):
				other = 3 - frm - to
				if frm == to:
					continue
				dp[i][frm][to] = dp[i - 1][frm][other] + matrix[frm][to] + dp[i - 1][other][to]
				c = dp[i - 1][frm][to] + matrix[frm][other] + dp[i - 1][to][frm] + matrix[other][to] + dp[i - 1][frm][to]
				dp[i][frm][to] = min(c, dp[i][frm][to])
	print(dp[n][0][2])
main()


costs = []
costs.append(list(map(int, input().split())))
costs.append(list(map(int, input().split())))
costs.append(list(map(int, input().split())))
n = int(input())
memo = {}

def cost_hanoi(n, from_, to_):
	if (n, from_, to_) in memo:
		return memo[n, from_, to_]
	rods = [1, 2, 3]
	other = [x for x in rods if x not in (from_, to_)][0]
	if n == 1:
		return min(costs[from_ - 1][to_ - 1], costs[from_ - 1][other - 1] + costs[other - 1][to_ - 1])
	tactic1_s1 = min(cost_hanoi(n - 1, from_, other), cost_hanoi(n - 1, from_, to_) + cost_hanoi(n - 1, to_, other))
	tactic1_s2 = costs[from_ - 1][to_ - 1]
	tactic1_s3 = min(cost_hanoi(n - 1, other, to_), cost_hanoi(n - 1, other, from_) + cost_hanoi(n - 1, from_, to_))
	tactic1 = tactic1_s1 + tactic1_s2 + tactic1_s3
	tactic2_s1 = min(cost_hanoi(n - 1, from_, to_), cost_hanoi(n - 1, from_, other) + cost_hanoi(n - 1, other, to_))
	tactic2_s2 = costs[from_ - 1][other - 1]
	tactic2_s3 = cost_hanoi(n - 1, to_, from_)
	tactic2_s4 = costs[other - 1][to_ - 1]
	tactic2_s5 = min(cost_hanoi(n - 1, from_, to_), cost_hanoi(n - 1, from_, other) + cost_hanoi(n - 1, other, to_))
	tactic2 = tactic2_s1 + tactic2_s2 + tactic2_s3 + tactic2_s4 + tactic2_s5
	res = min(tactic1, tactic2)
	memo[n, from_, to_] = res
	return res
print(cost_hanoi(n, 1, 3))


def play(price, n):
	dp = [[[0 for k in range(0, 4)] for j in range(0, 4)] for i in range(0, n + 1)]
	for i in range(1, n + 1):
		for j in range(1, 4):
			for k in range(1, 4):
				dp[i][j][k] = min(dp[i - 1][j][k ^ j] + price[j][k] + dp[i - 1][k ^ j][k], 2 * dp[i - 1][j][k] + price[j][k ^ j] + dp[i - 1][k][j] + price[j ^ k][k])
	return dp[n][1][3]

def main():
	matrix = [[0 for j in range(0, 4)] for i in range(0, 4)]
	i = 1
	while i < 4:
		row = list(map(int, input().split()))
		for j in range(1, 4):
			matrix[i][j] = row[j - 1]
		i += 1
	n = int(input())
	print(play(matrix, n))
main()