結果

問題 No.2423 Merge Stones
ユーザー startcppstartcpp
提出日時 2023-08-12 14:08:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 451 ms / 4,000 ms
コード長 1,213 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 65,152 KB
実行使用メモリ 6,784 KB
最終ジャッジ日時 2024-04-30 05:08:43
合計ジャッジ時間 24,325 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 436 ms
6,784 KB
testcase_12 AC 209 ms
6,656 KB
testcase_13 AC 431 ms
6,656 KB
testcase_14 AC 304 ms
6,656 KB
testcase_15 AC 251 ms
6,784 KB
testcase_16 AC 382 ms
6,656 KB
testcase_17 AC 305 ms
6,656 KB
testcase_18 AC 380 ms
6,656 KB
testcase_19 AC 207 ms
6,784 KB
testcase_20 AC 302 ms
6,784 KB
testcase_21 AC 425 ms
6,784 KB
testcase_22 AC 310 ms
6,656 KB
testcase_23 AC 251 ms
6,784 KB
testcase_24 AC 253 ms
6,656 KB
testcase_25 AC 248 ms
6,784 KB
testcase_26 AC 251 ms
6,656 KB
testcase_27 AC 353 ms
6,656 KB
testcase_28 AC 442 ms
6,656 KB
testcase_29 AC 397 ms
6,656 KB
testcase_30 AC 304 ms
6,528 KB
testcase_31 AC 311 ms
6,784 KB
testcase_32 AC 421 ms
6,784 KB
testcase_33 AC 245 ms
6,656 KB
testcase_34 AC 341 ms
6,784 KB
testcase_35 AC 302 ms
6,784 KB
testcase_36 AC 302 ms
6,784 KB
testcase_37 AC 253 ms
6,656 KB
testcase_38 AC 245 ms
6,784 KB
testcase_39 AC 437 ms
6,528 KB
testcase_40 AC 395 ms
6,656 KB
testcase_41 AC 354 ms
6,656 KB
testcase_42 AC 441 ms
6,784 KB
testcase_43 AC 205 ms
6,528 KB
testcase_44 AC 451 ms
6,656 KB
testcase_45 AC 248 ms
6,656 KB
testcase_46 AC 356 ms
6,656 KB
testcase_47 AC 247 ms
6,784 KB
testcase_48 AC 249 ms
6,528 KB
testcase_49 AC 216 ms
6,656 KB
testcase_50 AC 206 ms
6,656 KB
testcase_51 AC 395 ms
6,528 KB
testcase_52 AC 378 ms
6,656 KB
testcase_53 AC 420 ms
6,784 KB
testcase_54 AC 381 ms
6,784 KB
testcase_55 AC 446 ms
6,656 KB
testcase_56 AC 378 ms
6,656 KB
testcase_57 AC 399 ms
6,528 KB
testcase_58 AC 443 ms
6,784 KB
testcase_59 AC 442 ms
6,656 KB
testcase_60 AC 394 ms
6,656 KB
testcase_61 AC 446 ms
6,528 KB
testcase_62 AC 443 ms
6,656 KB
testcase_63 AC 445 ms
6,656 KB
testcase_64 AC 437 ms
6,784 KB
testcase_65 AC 434 ms
6,528 KB
testcase_66 AC 429 ms
6,656 KB
testcase_67 AC 445 ms
6,784 KB
testcase_68 AC 430 ms
6,656 KB
testcase_69 AC 428 ms
6,528 KB
testcase_70 AC 432 ms
6,656 KB
testcase_71 AC 425 ms
6,528 KB
testcase_72 AC 421 ms
6,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//bitで作れる色の集合を持つと間に合いそう
#include <iostream>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int n, K;
int a[600], ra[601];
int c[600];
int dp[601][601];
bool memo[601][601];

//[l, r) -> return: x (ibit目が1なら色i(>=0)にできる, 0なら色i(>=0)にできない)
int dfs(int l, int r) {
	if (r - l == 1) { return (1LL << c[l]); }
	if (memo[l][r]) { return dp[l][r]; }

	int ret = 0;
	for (int i = l + 1; i < r; i++) {
		int res1 = dfs(l, i);
		int res2 = dfs(i, r);
		
		int res = 0;
		for (int j = 0; j <= K; j++) {
			res |= res1 & (res2 << j);
			res |= res2 & (res1 << j);
		}
		for (int j = 0; j <= K; j++) {
			res |= res1 & (res2 >> j);
			res |= res2 & (res1 >> j);
		}
		ret |= res;
	}
	memo[l][r] = true;
	return dp[l][r] = ret;
}

signed main() {
	int i, j;

	cin >> n >> K;
	rep(i, n) cin >> a[i];
	rep(i, n) a[n + i] = a[i];
	rep(i, 2 * n) ra[i + 1] = ra[i] + a[i];
	rep(i, n) { cin >> c[i]; c[i]--; }
	rep(i, n) c[n + i] = c[i];

	int ans = 0;
	rep(i, n) {
		for (j = 1; j <= n; j++) {
			int res = dfs(i, i + j);
			if (res > 0) {
				ans = max(ans, ra[i + j] - ra[i]);
			}
		}
	}

	cout << ans << endl;
	return 0;
}
0