結果

問題 No.545 ママの大事な二人の子供
ユーザー finefine
提出日時 2017-07-15 19:26:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 974 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 170,020 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 21:51:18
合計ジャッジ時間 2,714 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll INF = 1000000000000000LL;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<ll> a(n), b(n);
	for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
	int n1 = n / 2, n2 = n - n1;

	vector<ll> v1(1 << n1, 0);
	for (int i = 0; i < (1 << n1); i++) {
		for (int j = 0; j < n1; j++) {
			if (i & (1 << j)) {
				v1[i] += a[j];
			} else {
				v1[i] -= b[j];
			}
		}
	}
	sort(v1.begin(), v1.end());

	vector<ll> v2(1 << n2, 0);
	for (int i = 0; i < (1 << n2); i++) {
		for (int j = 0; j < n2; j++) {
			if (i & (1 << j)) {
				v2[i] += a[n1 + j];
			} else {
				v2[i] -= b[n1 + j];
			}
		}
	}
	sort(v2.begin(), v2.end());

	ll ans = INF;
	for (ll x : v1) {
		int idx = lower_bound(v2.begin(), v2.end(), -x) - v2.begin();
		ans = min(ans, abs(x + v2[idx]));
		if (ans == 0) break;
		if (idx > 0) ans = min(ans, abs(x + v2[idx - 1]));
	}
	cout << ans << endl;
	return 0;
}
0