結果
| 問題 | No.545 ママの大事な二人の子供 | 
| コンテスト | |
| ユーザー |  vjudge1 | 
| 提出日時 | 2025-03-14 23:15:09 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,296 bytes | 
| コンパイル時間 | 1,928 ms | 
| コンパイル使用メモリ | 164,388 KB | 
| 実行使用メモリ | 7,324 KB | 
| 最終ジャッジ日時 | 2025-03-14 23:15:13 | 
| 合計ジャッジ時間 | 3,511 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 18 WA * 14 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 40;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll n;
ll a[N], b[N];
ll f[1 << 17];
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cin >> n;
    for (ll i = 1; i <= n; i++)
        cin >> a[i] >> b[i];
    ll full = (1 << (n >> 1));
    for (ll S = 0; S < full; S++)
    {
        ll sum1 = 0, sum2 = 0;
        for (ll i = 0; i < (n >> 1); i++)
        {
            if ((S >> i) & 1)
                sum1 += a[i + 1];
            else
                sum2 += b[i + 1];
        }
        f[S + 1] = sum1 - sum2;
    }
    sort(f + 1, f + 1 + full);
    ll ans = inf;
    for (ll S = 0; S < (1 << (n - (n >> 1))); S++)
    {
        ll sum1 = 0, sum2 = 0;
        for (ll i = 0; i < n - (n >> 1); i++)
        {
            if ((S >> i) & 1)
                sum1 += a[(n >> 1) + i + 1];
            else
                sum2 += b[(n >> 1) + i + 1];
        }
        ll tmp = sum1 - sum2;
        ll x = lower_bound(f + 1, f + 1 + full, -tmp) - f;
        ans = min(ans, abs(tmp + f[x]));
        x = upper_bound(f + 1, f + 1 + full, -tmp) - f;
        ans = min(ans, abs(tmp + f[x]));
    }
    cout << ans << endl;
    return 0;
}
            
            
            
        