結果

問題 No.783 門松計画
ユーザー leaf_1415leaf_1415
提出日時 2019-01-11 22:13:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,211 bytes
コンパイル時間 351 ms
コンパイル使用メモリ 52,884 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-30 18:39:05
合計ジャッジ時間 1,512 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 22 ms
4,380 KB
testcase_12 AC 10 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

using namespace std;

int n, c;
int l[55], w[55];
int memo[55][55][55];

int calc(int x, int pp, int p)
{
	if(memo[x][pp][p] != -1) return memo[x][pp][p];
	
	int ret = 0;
	for(int i = 1; i <= n; i++){
		if(pp < p && l[i] > p) continue;
		if(pp > p && l[i] < p) continue;
		if(l[i] == p || l[i] == pp) continue;
		if(x+w[i] > c) continue;
		ret = max(ret, calc(x+w[i], p, l[i]) + l[i]);
	}
	return memo[x][pp][p] = ret;
}

bool check(int p, int q)
{
	int x = l[p], y = l[q];
	for(int i = 1; i <= n; i++){
		if(x == l[i] || y == l[i]) continue;
		if(w[p]+w[q]+w[i] > c) continue;
		if(x < y && y > l[i]) return true;
		if(x > y && y < l[i]) return true;
	}
	return false;
}

int main(void)
{
	cin >> n >> c;
	for(int i = 1; i <= n; i++) cin >> l[i];
	for(int i = 1; i <= n; i++) cin >> w[i];
	
	for(int i = 0; i < 55; i++){
		for(int j = 0; j < 55; j++){
			for(int k = 0; k < 55; k++){
				memo[i][j][k] = -1;
			}
		}
	}
	
	int ans = 0;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(l[i] == l[j]) continue;
			if(w[i]+w[j] > c) continue;
			if(!check(i, j)) continue;
			ans = max(ans, calc(w[i]+w[j], l[i], l[j]) + l[i]+l[j]);
		}
	}
	cout << ans << endl;
	return 0;
}
0