結果
問題 |
No.2423 Merge Stones
|
ユーザー |
|
提出日時 | 2023-08-12 18:19:13 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 259 ms / 4,000 ms |
コード長 | 2,336 bytes |
コンパイル時間 | 1,894 ms |
コンパイル使用メモリ | 179,496 KB |
実行使用メモリ | 5,632 KB |
最終ジャッジ日時 | 2024-11-20 08:37:17 |
合計ジャッジ時間 | 17,005 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 72 |
ソースコード
#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; }