結果

問題 No.4 おもりと天秤
ユーザー Kenichi Higashide
提出日時 2016-07-07 12:08:30
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 642 bytes
コンパイル時間 655 ms
コンパイル使用メモリ 71,612 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-26 09:36:24
合計ジャッジ時間 1,469 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	int n;
	cin >> n;
	vector<int> w(n);
	int weightSum = 0;
	for (int i = 0; i < n; ++i) {
		cin >> w[i];
		weightSum += w[i];
	}
	if (weightSum % 2 == 1) {
		cout << "impossible" << endl;
		return 0;
	}
	
	vector<bool> weightPossible(10001);
	weightPossible[0] = true;
	
	for (int i = 0; i < n; ++i) {
		for (int j = 10000; j >= w[i]; --j) {
			if (weightPossible[j - w[i]]) {
				weightPossible[j] = true;
			}
		}
	}

	if (weightPossible[weightSum / 2]) {
		cout << "possible" << endl;
	} else {
		cout << "impossible" << endl;
	}

	return 0;
}
0