結果

問題 No.572 妖精の演奏
ユーザー chocoruskchocorusk
提出日時 2018-12-11 23:08:30
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 1,418 ms / 2,000 ms
コード長 1,508 bytes
コンパイル時間 1,102 ms
コンパイル使用メモリ 98,164 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-24 07:00:20
合計ジャッジ時間 13,066 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
	ll n; int m;
	cin>>n>>m;
	int a[30][30];
	for(int i=0; i<m; i++){
		for(int j=0; j<m; j++){
			cin>>a[i][j];
		}
	}
	int dp[31][31][31];
	for(int i=0; i<m; i++){
		for(int j=0; j<m; j++){
			for(int k=0; k<=m; k++){
				dp[i][j][k]=-1;
			}
		}
	}
	for(int i=0; i<m; i++){
		dp[i][i][0]=0;
		for(int j=0; j<m; j++){
			for(int k=0; k<m; k++){
				if(dp[i][k][j]==-1) continue;
				for(int l=0; l<m; l++){
					dp[i][l][j+1]=max(dp[i][l][j+1], dp[i][k][j]+a[k][l]);
				}
			}
		}
	}
	ll ans=0;
	for(int i=0; i<m; i++){
		for(int j=0; j<m; j++){
			for(int k=0; k<m; k++){
				for(ll x=0; x<=m; x++){
					if(dp[i][k][x]==-1) continue;
					if(n-1-x<0) continue;
					for(ll y=0; y<=m; y++){
						if(dp[k][k][y]==-1) continue;
						if(y==0){
							if(n-1-x>m) continue;
							ans=max(ans, (ll)dp[i][k][x]+(ll)dp[k][j][n-1-x]);
							continue;
						}
						for(ll z=(n-1-x)%y; z<=m; z+=y){
							if(dp[k][j][z]==-1) continue;
							if(n-1<x+z) continue;
							ll c=(n-1-x-z)/y;
							ans=max(ans, (ll)dp[i][k][x]+(ll)dp[k][j][z]+c*(ll)dp[k][k][y]);
						}
					}
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}
0