結果

問題 No.2423 Merge Stones
ユーザー umimel
提出日時 2023-08-12 14:39:00
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,715 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 177,884 KB
実行使用メモリ 161,664 KB
最終ジャッジ日時 2024-11-19 20:39:49
合計ジャッジ時間 218,858 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 54 TLE * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60;
const int IINF = (1 << 30) - 1;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int N, K; cin >> N >> K;
    vector<ll> A(N);
    vector<int> C(N);
    for(int i=0; i<N; i++) cin >> A[i];
    for(int i=0; i<N; i++){
        cin >> C[i];
        C[i]--;
    }

    vector<vector<vector<ll>>> dp(N, vector<vector<ll>>(N, vector<ll>(50, -1)));
    vector<vector<vector<ll>>> idp(N, vector<vector<ll>>(N, vector<ll>(50, -1)));
    //初期化
    for(int i=0; i<N; i++){
        dp[i][i][C[i]] = A[i];

        for(int c=0; c<50; c++){
            for(int j=0; j<=K; j++){
                if(c-j>=0) idp[i][i][c]=max(idp[i][i][c], dp[i][i][c-j]);
            }
        }
        //rdpの更新
        for(int c=0; c<50; c++){
            for(int j=0; j<=K; j++){
                if(c+j<50) idp[i][i][c]=max(idp[i][i][c], dp[i][i][c+j]);
            }
        }
    }

    //遷移
    for(int d=2; d<=N; d++){
        for(int l=0; l<N; l++){
            //[l, (l+d-1)%N]の解を求める
            int r = (l+d-1)%N;
            for(int m=l; m<l+d-1; m++){
                int mmod = m%N;
                //[l, mmod]と[mmod+1, (l+d-1)%N]をマージ
                for(int c=0; c<50; c++){
                    if(dp[l][mmod][c]!=-1 && idp[(mmod+1)%N][r][c]!=-1){
                        dp[l][r][c] = dp[l][mmod][c]+idp[(mmod+1)%N][r][c];
                    }
                    if(dp[(mmod+1)%N][r][c]!=-1 && idp[l][mmod][c]!=-1){
                        dp[l][r][c] = dp[(mmod+1)%N][r][c]+idp[l][mmod][c];
                    }
                }
            }

            //ldpの更新
            for(int c=0; c<50; c++){
                for(int i=0; i<=K; i++){
                    if(c-i>=0) idp[l][r][c]=max(idp[l][r][c], dp[l][r][c-i]);
                }
            }
            //rdpの更新
            for(int c=0; c<50; c++){
                for(int i=0; i<=K; i++){
                    if(c+i<50) idp[l][r][c]=max(idp[l][r][c], dp[l][r][c+i]);
                }
            }
        }
    }

    ll ans = -LINF;
    for(int l=0; l<N; l++) for(int r=0; r<N; r++) for(int c=0; c<50; c++){
        ans = max(ans, dp[l][r][c]);
    }

    cout << ans << endl;
}
0