Hungry Canadian Gym - 101484G

Hungry Canadian

 Gym - 101484G 

 

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

https://vjudge.net/contest/328615#problem/G

題意:給出把i字母放到j字母后所花的費用的矩陣(26×26)

求出長度爲n的花費最小的字符串

 

dp[ i ][ j ]爲長度爲i , 以j字母結尾的花費最小值

dp[ 1 ][ i ] = 0               

dp[ i ][ j ] = min(dp[ i ][ j ],dp[ i-1 ][ k ]+map[ k ][ j ]         

ans = min(dp[ n ][ i ])

#include<bits/stdc++.h>
using namespace std;

long long a[50][50];
long long dp[10200][30];

int main()
{
    long long i,j,n,k,ans;
    ans = 0x3f3f3f3f;
    scanf("%lld",&n);
    memset(dp,0x3f3f3f3f,sizeof(dp));

    for(i = 1; i <= 26; i ++)
    {
        for(j = 1; j <= 26; j ++)
        {
        
            scanf("%lld",&a[i][j]);
        }
    }
    for(i = 1; i <= 26; i ++)
    {
        dp[1][i] = 0;
    }


    for(i = 2; i<= n; i ++)
    {
        for(k = 1; k <= 26; k ++)
        {
            for(j = 1; j <= 26; j ++)
                dp[i][j] = min(dp[i][j],dp[i-1][k]+a[k][j]);
        }
    }

    for(i = 1; i <= 26; i ++)
    {
        ans = min(ans,dp[n][i]);
    }


    printf("%lld\n",ans);

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章