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;
}

 

### 关于 UniApp 框架推荐资源与教程 #### 1. **Uniapp 官方文档** 官方文档是最权威的学习资料之一,涵盖了从基础概念到高级特性的全方位讲解。对于初学者来说,这是了解 UniApp 架构技术细节的最佳起点[^3]。 #### 2. **《Uniapp 从入门到精通:案例分析与最佳实践》** 该文章提供了系统的知识体系,帮助开发者掌握 Uniapp 的基础知识、实际应用以及开发过程中的最佳实践方法。它不仅适合新手快速上手,也能够为有经验的开发者提供深入的技术指导[^1]。 #### 3. **ThorUI-uniapp 开源项目教程** 这是一个专注于 UI 组件库设计实现的教学材料,基于 ThorUI 提供了一系列实用的功能模块。通过学习此开源项目的具体实现方式,可以更好地理解如何高效构建美观且一致的应用界面[^2]。 #### 4. **跨平台开发利器:UniApp 全面解析与实践指南** 这篇文章按照章节形式详细阐述了 UniApp 的各个方面,包括但不限于其工作原理、技术栈介绍、开发环境配置等内容,并附带丰富的实例演示来辅助说明理论知识点。 以下是几个重要的主题摘选: - **核心特性解析**:解释了跨端运行机制、底层架构组成及其主要功能特点。 - **开发实践指南**:给出了具体的页面编写样例代码,展示了不同设备间 API 调用的方法论。 - **性能优化建议**:针对启动时间缩短、图形绘制效率提升等方面提出了可行策略。 ```javascript // 示例代码片段展示条件编译语法 export default { methods: { showPlatform() { console.log(process.env.UNI_PLATFORM); // 输出当前平台名称 #ifdef APP-PLUS console.log('Running on App'); #endif #ifdef H5 console.log('Running on Web'); #endif } } } ``` #### 5. **其他补充资源** 除了上述提到的内容外,还有许多在线课程视频可供选择,比如 Bilibili 上的一些免费系列讲座;另外 GitHub GitCode 平台上也有不少优质的社区贡献作品值得借鉴研究。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值