結果

問題 No.4 おもりと天秤
ユーザー Mr.Spock
提出日時 2020-12-03 16:26:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 874 bytes
コンパイル時間 1,613 ms
コンパイル使用メモリ 170,640 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-14 02:04:57
合計ジャッジ時間 2,443 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	vector<int>v(n);
	int sum = 0;
	for (int i = 0; i < n; i++) {
		cin >> v[i];
		sum += v[i];
	}
	if (sum & 1) {
		cout << "impossible" << endl;
		exit(0);
	}
	if (n < 20) {
		for (int mask = 0; mask < (1 << n); mask++) {
			int s = 0;
			for (int i = 0; i < n; i++) {
				if (mask & (1 << i)) {
					s += v[i];
				}
				if (s == sum / 2) {
					cout << "possible" << endl;
					exit(0);
				}
			}
		}
		cout << "impossible" << endl;
		exit(0);
	}
	vector<bool>dp(sum + 1, false);
	dp[sum] = true;
	for (int i = 0; i < n; i++) {
		for (int j = sum; j >= 0; j--) {
			if (dp[j] && j - v[i] >= 0) {
				dp[j - v[i]] = true;
			}
		}
	}
	if (dp[sum / 2])
		cout << "possible" << endl;
	else
		cout << "impossible" << endl;
}

0