問題一覧 > 通常問題

No.572 The Fairy's Performance

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 56
作問者 : horiesiniti / テスター : はむこ
お気に入りにしたユーザー ProblemId : 1216 / 自分の提出
問題文最終更新日: 2026-06-25 13:22:32
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 174 (順位表) の他の問題:

Problem Statement

A fairy performs by following a musical score. The score is a sequence of notes placed one at a time, and playing a certain note right after a certain note earns a fixed score. You are given the number n of notes to write in the score, the number m of kinds of notes, and a table showing how much score is added when a certain note is played right after a certain note. From these two pieces of data, find the score obtained when writing the musical score that gives the maximum score. A musical score is evaluated by the simple sum of the scores.

Input

$n$
$m$
$A_{11}$$\dots$$A_{1m}$
$\dots$
$A_{m1}$$\dots$$A_{mm}$

$1 \leq n \leq 5 \times 10^{9}$
$2 \leq m \leq 30$
$0 < A_{i,j} \leq100$
$A_{i,j}$ means that playing the note of kind j after the note of kind i earns a score of $A_{i,j}$ points.
However, since the fairy dislikes consecutive identical notes, $A_{i,i}$=0
You may assume that $A_{i,j}$ is an integer.

Output

Print a newline at the end.

Sample

Sample 1
Input
7
4
0 20 10 100
10 0 100 10
100 10 0 10
10 100 20 0
Output
600

It is a repetition of the notes 1 4 2 3.

Sample 2
Input
177
10
0 1 2 9 10 10 6 7 10 5
4 0 5 3 5 1 4 4 10 4
8 7 0 8 2 5 5 2 6 6
8 7 3 0 5 1 6 5 8 2
1 6 1 7 0 9 8 5 3 5
1 2 7 4 9 0 10 5 5 9
3 3 8 6 8 8 0 9 2 10
2 5 7 7 1 9 7 0 6 10
9 1 4 4 5 7 6 4 0 3
1 4 3 8 4 9 1 1 3 0
Output
1702

Sample 3
Input
123
12
0 45 21 74 60 97 45 10 71 70 76 8
57 0 1 13 49 23 61 58 52 35 39 34
89 53 0 95 86 61 72 98 88 78 47 77
66 7 42 0 25 14 23 11 25 34 94 53
60 57 41 16 0 86 10 23 64 23 53 76
78 7 48 37 30 0 18 87 73 7 51 31
99 57 65 96 83 36 0 73 33 84 28 74
69 59 62 47 6 33 55 0 19 13 34 25
14 32 97 17 47 20 46 23 0 58 32 68
28 50 74 74 32 25 63 84 41 0 80 20
44 42 1 95 68 32 8 1 71 91 0 11
54 86 48 84 53 80 48 45 5 90 26 0
Output
11532

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。