結果

問題 No.4 おもりと天秤
ユーザー masamasa
提出日時 2015-01-18 06:34:31
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 689 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 67,700 KB
実行使用メモリ 7,536 KB
最終ジャッジ日時 2023-09-04 21:34:21
合計ジャッジ時間 8,091 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

int n;
vector<int> w;
int half;

bool add(int sum, int idx) {
	if (sum == half) {
		return true;
	} else if (sum > half) {
		return false;
	}

	for (int i = idx + 1; i < n; i++) {
		if(add(sum + w[idx], i)) {
			return true;
		}
	}
	return false;
}

int main() {

	cin >> n;
	int total = 0;

	w.assign(n, 0);
	for (int i = 0; i < n; i++) {
		cin >> w[i];
		total += w[i];
	}

	sort(w.begin(), w.end(), greater<int>());

	bool ret = false;
	if (total % 2 == 0) {
		half = total / 2;
		ret = add(0, 0);
	}

	cout << (ret ? "possible" : "impossible") << endl;
	return 0;
}
0