Teodoro just moved to Canada. He is having a hard time there as the cost of living is higher than he expected. He is short on money so he decided to eat a string, as they are cheaper than food. He knows exactly how hungry he is, so he will buy a string with exactly K letters.
Teodoro went to a string store and saw that the price of a string depends on which letters are adjacent. For example, the price of the string "bca" is equal to the cost of putting "c" after "b" and "a" after "c". The costs of adjacent letters are given in a 26 × 26 matrix P, in which pi, j corresponds to the cost of putting the j-th letter of the alphabet after the i-th letter of the alphabet, e.g., p3, 7 is the cost of putting "g" after "c". Notice that the cost of putting i after j may not be the same as putting j after i, that is, pi, j may be different from pj, i.
Teodoro wants to order a string of size K with the minimum possible price. He is so hungry that he can barely think. Please help him find the cheapest string of size K.
Input
The first line of the input contains an integer K (2 ≤ K ≤ 104), indicating the size of the string Teodoro will order.
The next 26 lines contain matrix P. The j-th integer of the i-th line corresponds to pi, j (0 ≤ pi, j ≤ 103), indicating the cost of putting the j-th letter of the alphabet right after the i-th letter of the alphabet.
Output
Output a single integer: the minimum cost to create a string of size K.
Example
Input
4
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 2 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 1 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 3 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
Output
6
思路:dp动归转移,dp[i][j][len]表示长度为len,以aij结束的串。aij是由axi转移过去的,也就是dp[i][j][len]是由dp[x][i][len-1]转移过去的,x从1到26都走一遍,找一个最小的dp[x][i][len-1]。
起始状态:每个串都是从aij开始的,也就是长度至少都为2,所以起始dp为dp[i][j][2],也就是题中给的s[i][j]。
#include<bits/stdc++.h>
using namespace std;
long long dp[30][30][10010];
int main()
{
int i,j,a,k,x;
int s[30][30];
scanf("%d",&a);
for(i=1;i<=26;i++)
{
for(j=1;j<=26;j++)
{
scanf("%d",&s[i][j]);
}
}
memset(dp,0x3f3f3f3f,sizeof(dp));
for(i=1;i<=26;i++)
{
for(j=1;j<=26;j++)
{
dp[i][j][2]=s[i][j];
}
}
for(i=3;i<=a;i++)
{
for(j=1;j<=26;j++)
{
for(k=1;k<=26;k++)
{
for(x=1;x<=26;x++)
{
dp[j][k][i]=min(dp[j][k][i],dp[x][j][i-1]+s[j][k]);
}
}
}
}
int ans=0x3f3f3f3f;
for(i=1;i<=26;i++)
{
for(j=1;j<=26;j++)
{
ans=min(dp[i][j][a],ans);
}
}
printf("%d\n",ans);
return 0;
}