結果

問題 No.2423 Merge Stones
ユーザー 👑 emthrmemthrm
提出日時 2023-08-12 14:40:15
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 4,000 ms
コード長 1,883 bytes
コンパイル時間 3,540 ms
コンパイル使用メモリ 252,212 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2024-04-30 06:46:49
合計ジャッジ時間 5,679 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 45 ms
8,704 KB
testcase_12 AC 20 ms
8,832 KB
testcase_13 AC 21 ms
8,704 KB
testcase_14 AC 21 ms
8,832 KB
testcase_15 AC 19 ms
8,832 KB
testcase_16 AC 20 ms
8,832 KB
testcase_17 AC 20 ms
8,832 KB
testcase_18 AC 20 ms
8,832 KB
testcase_19 AC 19 ms
8,832 KB
testcase_20 AC 20 ms
8,704 KB
testcase_21 AC 20 ms
8,832 KB
testcase_22 AC 20 ms
8,704 KB
testcase_23 AC 21 ms
8,704 KB
testcase_24 AC 20 ms
8,704 KB
testcase_25 AC 21 ms
8,832 KB
testcase_26 AC 20 ms
8,832 KB
testcase_27 AC 21 ms
8,832 KB
testcase_28 AC 22 ms
8,832 KB
testcase_29 AC 22 ms
8,832 KB
testcase_30 AC 21 ms
8,832 KB
testcase_31 AC 21 ms
8,832 KB
testcase_32 AC 21 ms
8,704 KB
testcase_33 AC 20 ms
8,704 KB
testcase_34 AC 20 ms
8,832 KB
testcase_35 AC 20 ms
8,704 KB
testcase_36 AC 20 ms
8,832 KB
testcase_37 AC 19 ms
8,832 KB
testcase_38 AC 20 ms
8,704 KB
testcase_39 AC 21 ms
8,832 KB
testcase_40 AC 20 ms
8,832 KB
testcase_41 AC 21 ms
8,832 KB
testcase_42 AC 23 ms
8,704 KB
testcase_43 AC 20 ms
8,704 KB
testcase_44 AC 23 ms
8,832 KB
testcase_45 AC 21 ms
8,832 KB
testcase_46 AC 21 ms
8,704 KB
testcase_47 AC 20 ms
8,704 KB
testcase_48 AC 22 ms
8,832 KB
testcase_49 AC 20 ms
8,832 KB
testcase_50 AC 20 ms
8,704 KB
testcase_51 AC 44 ms
8,832 KB
testcase_52 AC 46 ms
8,832 KB
testcase_53 AC 46 ms
8,704 KB
testcase_54 AC 45 ms
8,832 KB
testcase_55 AC 54 ms
8,832 KB
testcase_56 AC 22 ms
8,704 KB
testcase_57 AC 21 ms
8,704 KB
testcase_58 AC 23 ms
8,704 KB
testcase_59 AC 23 ms
8,832 KB
testcase_60 AC 23 ms
8,832 KB
testcase_61 AC 46 ms
8,832 KB
testcase_62 AC 44 ms
8,832 KB
testcase_63 AC 45 ms
8,704 KB
testcase_64 AC 43 ms
8,704 KB
testcase_65 AC 44 ms
8,832 KB
testcase_66 AC 46 ms
8,832 KB
testcase_67 AC 50 ms
8,704 KB
testcase_68 AC 50 ms
8,704 KB
testcase_69 AC 50 ms
8,704 KB
testcase_70 AC 41 ms
8,832 KB
testcase_71 AC 33 ms
8,832 KB
testcase_72 AC 32 ms
8,704 KB
権限があれば一括ダウンロードができます

ソースコード

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 dp1(n * 2 - 1, vector(n * 2 - 1, 0LL)), dp2(n * 2 - 1, vector(n * 2 - 1, 0LL));
  const auto make_dp2 = [&](const int l, const int r) -> void {
    FOR(diff, 1, k + 1) dp2[l][r] |= dp1[l][r] >> diff;
    dp2[l][r] |= dp1[l][r];
    FOR(diff, 1, k + 1) dp2[l][r] |= dp1[l][r] << diff;
  };
  REP(r, n * 2 - 1) {
    dp1[r][r] = 1LL << c[r % n];
    make_dp2(r, r);
    for (int l = r - 1; l >= 0 && r - l + 1 <= n; --l) {
      FOR(m, l, r) {
        if (dp1[l][m] == 0 || dp1[m + 1][r] == 0) continue;
        dp1[l][r] |= (dp1[l][m] & dp2[m + 1][r]) | (dp2[l][m] & dp1[m + 1][r]);
      }
      make_dp2(l, r);
    }
  }
  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 (dp1[i][j] > 0) chmax(ans, magic);
    }
  }
  cout << ans << '\n';
  return 0;
}
0