結果

問題 No.545 ママの大事な二人の子供
ユーザー 0w10w1
提出日時 2017-08-18 18:13:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 982 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 199,776 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-04-22 15:50:12
合計ジャッジ時間 3,020 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 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 WA -
testcase_10 AC 1 ms
5,376 KB
testcase_11 WA -
testcase_12 AC 1 ms
5,376 KB
testcase_13 WA -
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 WA -
testcase_19 WA -
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 17 ms
5,376 KB
testcase_23 WA -
testcase_24 AC 6 ms
5,376 KB
testcase_25 AC 32 ms
6,528 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 32;

int N;
int A[MAXN], B[MAXN];

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i] >> B[i];
  }
  if (N == 1) {
    cout << min(A[0], B[0]) << endl;
    exit(0);
  }
  int hn = N / 2;
  set<long long> bag;
  for (int s = 0; s < 1 << hn; ++s) {
    long long diff = 0;
    for (int i = 0; i < hn; ++i) {
      if (s >> i & 1) {
        diff += A[i];
      } else {
        diff -= B[i];
      }
    }
    bag.emplace(diff);
  }
  long long ans = 1LL << 62;
  for (int s = 0; s < 1 << N - hn; ++s) {
    long long diff = 0;
    for (int i = 0; i < N - hn; ++i) {
      if (s >> i & 1) {
        diff += A[hn + i];
      } else {
        diff -= B[hn + i];
      }
    }
    auto it = bag.lower_bound(-diff);
    if (it != bag.begin()) --it;
    for (int i = 0; i < 3; ++i) {
      if (it == bag.end()) break;
      ans = min(ans, abs(diff + *it));
    }
  }
  cout << ans << endl;
  return 0;
}
0