結果

問題 No.572 妖精の演奏
ユーザー rpy3cpprpy3cpp
提出日時 2017-10-07 03:27:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,643 bytes
コンパイル時間 1,821 ms
コンパイル使用メモリ 178,160 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 18:41:21
合計ジャッジ時間 2,625 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,384 KB
testcase_09 AC 3 ms
4,384 KB
testcase_10 AC 11 ms
4,380 KB
testcase_11 AC 11 ms
4,384 KB
testcase_12 AC 11 ms
4,376 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 11 ms
4,380 KB
testcase_16 AC 11 ms
4,376 KB
testcase_17 AC 10 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long solve(long long n, int m, const vector<vector<int>> & A){
    vector<vector<vector<long long>>> dp(m, vector<vector<long long>>(m + 1, vector<long long>(m, 0)));
    for (int s = 0; s < m; ++s){
        for (int d = 1; d < m + 1; ++d){
            for (int g = 0; g < m; ++g){
                if (d == 1){
                    dp[s][d][g] = A[s][g];
                    continue;
                };
                for (int v = 0; v < m; ++v){
                    dp[s][d][g] = max(dp[s][d][g], dp[s][d - 1][v] + A[v][g]);
                }
            }
        }
    }
    vector<vector<long long>> tail(m, vector<long long>(m + 1, 0));
    for (int s = 0; s < m; ++s){
        for (int d = 1; d < m + 1; ++d){
            tail[s][d] = *max_element(begin(dp[s][d]), end(dp[s][d]));
        }
    }
    --n;
    long long record = 0;
    for (int s = 0; s < m; ++s){
        for (int d1 = 0; d1 < m and n - d1 >= 0; ++d1){
            for (int v = 0; v < m; ++v){
                for (int d2 = 1; d2 < m + 1; ++d2){
                    long long cycle = (n - d1)/(long long)d2;
                    auto d3 = n - d1 - cycle * d2;
                    long long score = dp[s][d1][v] + dp[v][d2][v] * cycle + tail[v][d3];
                    if (score > record) record = score;
                }
            }
        }
    }
    return record;
}



int main() {
    long long n;
    cin >> n;
    int m;
    cin >> m;
    vector<vector<int>> A(m, vector<int>(m, 0));
    for (auto & row : A){
        for (auto & v : row) cin >> v;
    }
    cout << solve(n, m, A) << endl;
    return 0;
}
0