結果

問題 No.2423 Merge Stones
ユーザー dyktr_06
提出日時 2023-06-28 14:24:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,398 bytes
コンパイル時間 1,932 ms
コンパイル使用メモリ 197,796 KB
最終ジャッジ日時 2025-02-15 02:51:51
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 8 WA * 2 TLE * 1 -- * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

bool dp[601][601][51];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k; cin >> n >> k;
    vector<long long> a(2 * n);
    vector<int> c(2 * n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        a[i + n] = a[i];
    }
    for(int i = 0; i < n; i++){
        cin >> c[i];
        c[i]--;
        c[i + n] = c[i];
    }

    vector<long long> sum(2 * n + 1);
    for(int i = 0; i < 2 * n; i++){
        sum[i + 1] = sum[i] + a[i];
    }

    for(int i = 0; i < 2 * n; i++){
        dp[i][i][c[i]] = true;
    }

    long long ans = 0;
    for(int len = 1; len <= n; len++){
        for(int l = 0; l < 2 * n - len + 1; l++){
            int r = l + len - 1;
            for(int i = l; i < r; i++){
                for(int cl = 0; cl < 50; cl++){
                    bool bitl = dp[l][i][cl];
                    for(int cr = max(0, cl - k); cr <= min(50, cl + k); cr++){
                        bool bitr = dp[i + 1][r][cr];
                        if(bitl && bitr){
                            dp[l][r][cl] = true;
                            dp[l][r][cr] = true;
                        }
                    }
                    if(dp[l][r][cl]){
                        ans = max(ans, sum[r + 1] - sum[l]);
                    }
                }
            }
        }
    }
    cout << ans << endl;
}
0