結果

問題 No.2423 Merge Stones
ユーザー umimelumimel
提出日時 2023-08-12 18:19:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 263 ms / 4,000 ms
コード長 2,336 bytes
コンパイル時間 1,880 ms
コンパイル使用メモリ 180,424 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-30 13:26:27
合計ジャッジ時間 16,463 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 245 ms
6,940 KB
testcase_12 AC 146 ms
6,944 KB
testcase_13 AC 246 ms
6,944 KB
testcase_14 AC 183 ms
6,940 KB
testcase_15 AC 164 ms
6,940 KB
testcase_16 AC 224 ms
6,940 KB
testcase_17 AC 185 ms
6,940 KB
testcase_18 AC 226 ms
6,944 KB
testcase_19 AC 140 ms
6,940 KB
testcase_20 AC 194 ms
6,944 KB
testcase_21 AC 263 ms
6,944 KB
testcase_22 AC 189 ms
6,944 KB
testcase_23 AC 169 ms
6,940 KB
testcase_24 AC 165 ms
6,940 KB
testcase_25 AC 169 ms
6,940 KB
testcase_26 AC 164 ms
6,940 KB
testcase_27 AC 202 ms
6,940 KB
testcase_28 AC 243 ms
6,944 KB
testcase_29 AC 223 ms
6,944 KB
testcase_30 AC 187 ms
6,940 KB
testcase_31 AC 184 ms
6,944 KB
testcase_32 AC 252 ms
6,940 KB
testcase_33 AC 176 ms
6,940 KB
testcase_34 AC 202 ms
6,944 KB
testcase_35 AC 187 ms
6,940 KB
testcase_36 AC 185 ms
6,944 KB
testcase_37 AC 168 ms
6,944 KB
testcase_38 AC 163 ms
6,940 KB
testcase_39 AC 243 ms
6,940 KB
testcase_40 AC 228 ms
6,940 KB
testcase_41 AC 204 ms
6,940 KB
testcase_42 AC 247 ms
6,944 KB
testcase_43 AC 141 ms
6,940 KB
testcase_44 AC 245 ms
6,944 KB
testcase_45 AC 167 ms
6,944 KB
testcase_46 AC 218 ms
6,944 KB
testcase_47 AC 168 ms
6,944 KB
testcase_48 AC 171 ms
6,944 KB
testcase_49 AC 147 ms
6,940 KB
testcase_50 AC 140 ms
6,944 KB
testcase_51 AC 223 ms
6,940 KB
testcase_52 AC 222 ms
6,940 KB
testcase_53 AC 245 ms
6,940 KB
testcase_54 AC 224 ms
6,940 KB
testcase_55 AC 243 ms
6,940 KB
testcase_56 AC 224 ms
6,940 KB
testcase_57 AC 220 ms
6,944 KB
testcase_58 AC 252 ms
6,944 KB
testcase_59 AC 254 ms
6,944 KB
testcase_60 AC 234 ms
6,940 KB
testcase_61 AC 246 ms
6,940 KB
testcase_62 AC 243 ms
6,940 KB
testcase_63 AC 241 ms
6,944 KB
testcase_64 AC 241 ms
6,944 KB
testcase_65 AC 244 ms
6,940 KB
testcase_66 AC 246 ms
6,940 KB
testcase_67 AC 244 ms
6,940 KB
testcase_68 AC 246 ms
6,940 KB
testcase_69 AC 238 ms
6,940 KB
testcase_70 AC 240 ms
6,940 KB
testcase_71 AC 244 ms
6,944 KB
testcase_72 AC 244 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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<bitset<50>>> dp(N, vector<bitset<50>>(N));
    vector<vector<bitset<50>>> itvdp(N, vector<bitset<50>>(N));
    vector<vector<ll>> sum(N, vector<ll>(N, 0));
    //初期化
    for(int i=0; i<N; i++){
        dp[i][i].set(C[i]);
        sum[i][i] = A[i];
        for(int c=0; c<50; c++){
            for(int j=0; j<=K; j++){
                if(c-j>=0) itvdp[i][i][c] = itvdp[i][i][c] | dp[i][i][c-j];
                if(c+j<50) itvdp[i][i][c] = itvdp[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;
            sum[l][r] = sum[l][(r-1+N)%N]+A[r];
            for(int m=l; m<l+d-1; m++){
                int mmod = m%N;
                //[l, mmod]と[mmod+1, (l+d-1)%N]をマージ
                dp[l][r] = dp[l][r] | (dp[l][mmod]&itvdp[(mmod+1)%N][r]);
                dp[l][r] = dp[l][r] | (dp[(mmod+1)%N][r]&itvdp[l][mmod]);
            }

            //itvdpの更新
            for(int c=0; c<50; c++){
                for(int j=0; j<=K; j++){
                    if(c-j>=0) itvdp[l][r][c] = itvdp[l][r][c] | dp[l][r][c-j];
                    if(c+j<50) itvdp[l][r][c] = itvdp[l][r][c] | dp[l][r][c+j];
                }
            }
        }
    }

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

    cout << ans << endl;
}
0