結果
| 問題 | No.545 ママの大事な二人の子供 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-08-27 19:38:00 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 97 ms / 2,000 ms | 
| コード長 | 1,172 bytes | 
| コンパイル時間 | 2,490 ms | 
| コンパイル使用メモリ | 81,208 KB | 
| 最終ジャッジ日時 | 2025-01-13 15:30:34 | 
| ジャッジサーバーID (参考情報) | judge4 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 32 | 
ソースコード
#include <iostream>
#include <vector>
#include <set>
using lint = long long;
constexpr lint INF = 1LL << 50;
lint asum = 0;
std::set<lint> calc(int n) {
    std::vector<lint> ds(n);
    for (auto& d : ds) {
        lint a, b;
        std::cin >> a >> b;
        asum += a;
        d = b + a;
    }
    std::set<lint> ret{-INF, INF};
    for (int b = 0; b < (1 << n); ++b) {
        lint sum = 0;
        for (int i = 0; i < n; ++i) {
            if ((b >> i) & 1) sum += ds[i];
        }
        ret.insert(sum);
    }
    return ret;
}
void solve() {
    int n;
    std::cin >> n;
    auto fs = calc(n / 2),
         bs = calc(n - n / 2);
    lint ans = INF;
    for (auto f : fs) {
        if (std::abs(f) == INF) continue;
        // f + b >= asum, as small as possible
        auto b = *bs.lower_bound(asum - f);
        ans = std::min(ans, std::abs(f + b - asum));
        // f + b <= asum, as large as possible
        b = *(--bs.upper_bound(asum - f));
        ans = std::min(ans, std::abs(f + b - asum));
    }
    std::cout << ans << "\n";
}
int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    solve();
    return 0;
}
            
            
            
        