/* -*- coding: utf-8 -*- * * 572.cc: No.572 妖精の演奏 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_M = 30; /* typedef */ typedef long long ll; typedef ll mat[MAX_M][MAX_M]; /* global variables */ mat a, c, tmp; /* subroutines */ inline void setmax(ll &a, ll b) { if (a < b) a = b; } void matmaxsum(int m, const mat a, const mat b, mat c) { for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) { c[i][j] = 0; for (int k = 0; k < m; k++) setmax(c[i][j], a[i][k] + b[k][j]); } } void matinit(mat a) { memset(a, 0, sizeof(mat)); } void matcopy(const mat a, mat b) { memcpy(b, a, sizeof(mat)); } /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) scanf("%lld", &a[i][j]); n--; matinit(c); while (n > 0) { if (n & 1) { matmaxsum(m, c, a, tmp); matcopy(tmp, c); } matmaxsum(m, a, a, tmp); matcopy(tmp, a); n >>= 1; } ll maxv = 0; for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) setmax(maxv, c[i][j]); printf("%lld\n", maxv); return 0; }