2020 年 “联想杯”全国高校程序设计在线邀请赛暨第三届上海理工大学程序设计竞赛

E. Eight Digital Games
PDF 题面可用
你可以在这里下载。

单点时限: 1.0 sec

内存限制: 256 MB

Setsuna has been obsessed with an electronic video game called “Eight Digital”. It is a puzzle game which is too difficult for Setsuna, so she has been stuck in the first level for a long time.

In the game, you have a string of length n containing only digits from 1 to 8. Let’s call this string S, Si denotes the i-th character of S.

At the end of the game, the game system will calculate the penalties of inversions, and will deduct your coins. Inversion is a pair (i,j) which satisfies both i<j and Si>Sj. The game system will deduct you PSi,Sj coins for each inversion (i,j).

For example, string 85511 will cost you 2×P8,5+2×P8,1+4×P5,1 coins, and string 1234 will cost you 0 coins.

Before the end of the game, you can do arbitrary times of the operation. Each operation is to select two different numbers x and y(1x,y8), then all x in the string will become y, and all y will become x and the game system will deduct you Cx,y coins.

For example, you can spend C1,3 to convert string 131233 into string 313211.

To help poor girl Setsuna, you are required to find the minimum cost of coins to finish the game.

输入格式

The first line contains one integer n(1n105).

The second line contains a string S. It is guaranteed that the length of S is n and S only contains digits from 1 to 8.

The next 8 lines contains 8 integers each, where the j-th integer of the i-th line is Pi,j(0Pi,j107). It is guaranteed that Pi,j=0 holds for ij.

The next 8 lines contains 8 integers each, where the j-th integer of the i-th line is Ci,j(0Ci,j1012). It is guaranteed that Ci,i=0 and Ci,j=Cj,i.

输出格式

Output one integer indicating the minimum cost.

样例

Input
5
54321
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1
1 1 0 1 1 1 1 1
1 1 1 0 1 1 1 1
1 1 1 1 0 1 1 1
1 1 1 1 1 0 1 1
1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 0
Output
2
Input
6
222112
0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2
1 0 2 2 2 2 2 2
2 2 0 2 2 2 2 2
2 2 2 0 2 2 2 2
2 2 2 2 0 2 2 2
2 2 2 2 2 0 2 2
2 2 2 2 2 2 0 2
2 2 2 2 2 2 2 0
Output
4

提示

In sample 1, you can spend 1 coin to convert 54321 into 14325. Then, spend another 1 coin to convert it into 12345.

In sample 2, you can spend 2 coins to convert 222112 into 222882, then end the game. The inversions (4,6) and (5,6) will cost you 2×P8,2=2 coins. So the total cost is 4 coins.