結果

問題 No.4 おもりと天秤
ユーザー yudedako
提出日時 2016-03-13 11:38:36
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 637 bytes
コンパイル時間 548 ms
コンパイル使用メモリ 61,524 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-26 09:27:09
合計ジャッジ時間 1,315 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>

bool dp(const std::vector<int> &weight) {
	int sum = 0;
	for (const auto &w : weight) {
		sum += w;
	}
	if (sum % 2 == 0) {
		std::vector<bool> memo(sum / 2 + 1, false);
		memo[0] = true;
		for (const auto &w : weight) {
			for (auto i = sum / 2 - w; i >= 0; --i) {
				if (memo[i]) {
					memo[i + w] = true;
				}
			}
		}
		return memo[sum / 2];
	}
	else {
		return false;
	}
}
int main() {
	int n;
	std::cin >> n;
	std::vector<int> weight(n);
	for (auto &w : weight) {
		std::cin >> w;
	}
	if (dp(weight)) {
		std::cout << "possible\n";
	}
	else {
		std::cout << "impossible\n";
	}
	return 0;
}
0