結果

問題 No.545 ママの大事な二人の子供
コンテスト
ユーザー T
提出日時 2026-10-21 20:39:04
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,869 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,320 ms
コンパイル使用メモリ 232,140 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-10-21 20:39:29
合計ジャッジ時間 4,652 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <unordered_map>
#include <map>
#include <cmath>
#include <climits>
#define ALL(variable) (variable).begin(), (variable).end()
#define GT(variableName, defaultValue, limit) for(int variableName = defaultValue; variableName < (int)limit; variableName++)
#define GTE(variableName, defaultValue, limit) for(int variableName = defaultValue; variableName <= (int)limit; variableName++)
#define LT(variableName, defaultValue, limit) for(int variableName = defaultValue; variableName > (int)limit; variableName--)
#define LTE(variableName, defaultValue, limit) for(int variableName = defaultValue; variableName >= (int)limit; variableName--)
using namespace std ;
int main(){
    long long n, answer;
    cin >> n;
    answer = LLONG_MAX;
    vector<long long> A(n), B(n);
    GT(i, 0, n){
        cin >> A[i] >> B[i];
    }

    vector<long long> upA(0), loA(0);
    GT(i, 0, pow(2, n/2)){
        long long memo = 0, _i = i;
        GT(j, 0, n/2)
        {
            if(_i%2 == 0){
                memo += A[j];
            }
            else{
                memo -= B[j];
            }
            _i = i>>1;
        }
        upA.push_back(memo);
    }
    GT(i, 0, pow(2, n-n/2)){
        long long memo = 0, _i = i;
        GT(j, 0, n-n/2)
        {
            if(_i%2 == 0){
                memo += A[j+n/2];
            }
            else{
                memo -= B[j+n/2];
            }
            _i = i>>1;
        }
        loA.push_back(memo);
    }
    sort(ALL(upA));
    sort(ALL(loA));
    GT(i, 0, upA.size()){
        long long _ = min(abs(upA[i] + loA[lower_bound(ALL(loA), -upA[i])-loA.begin()]), abs(upA[i] + loA[lower_bound(ALL(loA), -upA[i])-1-loA.begin()]));
        answer = min(answer, _);
    }
    cout << answer << endl;
}
0