結果

問題 No.2423 Merge Stones
ユーザー emthrm
提出日時 2023-08-12 14:31:59
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,815 bytes
コンパイル時間 3,057 ms
コンパイル使用メモリ 252,700 KB
実行使用メモリ 13,640 KB
最終ジャッジ日時 2024-11-19 19:59:11
合計ジャッジ時間 83,798 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 59 TLE * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  constexpr int C = 50;
  int n, k; cin >> n >> k;
  vector<int> a(n), c(n);
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> c[i], --c[i];
  vector dp(n * 2 - 1, vector(n * 2 - 1, 0LL));
  REP(r, n * 2 - 1) {
    dp[r][r] = 1LL << c[r % n];
    for (int l = r - 1; l >= 0 && r - l + 1 <= n; --l) {
      FOR(m, l, r) {
        if (dp[m + 1][r] == 0) continue;
        for (ll index = dp[l][m]; index > 0;) {
          const uint64_t lsb = index & -index;
          const int i = countr_zero(lsb);
          for (int j = max(i - k, 0); j <= i + k && j < C; ++j) {
            if (dp[m + 1][r] >> j & 1) dp[l][r] |= (1LL << i) | (1LL << j);
          }
          index ^= lsb;
        }
      }
    }
  }
  ll ans = 0;
  REP(i, n * 2 - 1) {
    ll magic = 0;
    for (int j = i; j - i + 1 <= n && j < n * 2 - 1; ++j) {
      magic += a[j % n];
      if (dp[i][j] > 0) chmax(ans, magic);
    }
  }
  cout << ans << '\n';
  return 0;
}
0