結果

問題 No.2423 Merge Stones
ユーザー umimelumimel
提出日時 2023-08-12 14:31:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,896 bytes
コンパイル時間 1,857 ms
コンパイル使用メモリ 178,312 KB
実行使用メモリ 126,236 KB
最終ジャッジ日時 2024-04-30 06:22:23
合計ジャッジ時間 7,914 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
126,236 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
権限があれば一括ダウンロードができます

ソースコード

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>>> ldp(N, vector<vector<ll>>(N, vector<ll>(50, -1)));
    vector<vector<vector<ll>>> rdp(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) ldp[i][i][c]=max(ldp[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) rdp[i][i][c]=max(rdp[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]の解を求める
            ll 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 && max(ldp[(mmod+1)%N][r][c], rdp[(mmod+1)%N][r][c])!=-1){
                        dp[l][r][c] = dp[l][mmod][c]+max(ldp[(mmod+1)%N][r][c], rdp[(mmod+1)%N][r][c]);
                    }
                    if(dp[(mmod+1)%N][r][c]!=-1 && max(ldp[l][mmod][c], rdp[l][mmod][c])!=-1){
                        dp[l][r][c] = dp[(mmod+1)%N][r][c]+max(ldp[l][mmod][c], rdp[l][mmod][c]);
                    }
                }
            }

            //ldpの更新
            for(int c=0; c<50; c++){
                for(int i=0; i<=K; i++){
                    if(c-i>=0) ldp[l][r][c]=max(ldp[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) rdp[l][r][c]=max(rdp[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