結果

問題 No.545 ママの大事な二人の子供
ユーザー t-yoshiharat-yoshihara
提出日時 2017-07-14 23:35:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,167 bytes
コンパイル時間 1,586 ms
コンパイル使用メモリ 164,216 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 20:35:52
合計ジャッジ時間 2,576 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 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 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 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 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 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 13 ms
5,376 KB
testcase_23 AC 25 ms
5,376 KB
testcase_24 AC 12 ms
5,376 KB
testcase_25 AC 23 ms
5,376 KB
testcase_26 AC 25 ms
5,376 KB
testcase_27 AC 22 ms
5,376 KB
testcase_28 AC 25 ms
5,376 KB
testcase_29 AC 22 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>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
#define LL long long

using namespace std;

int main(){

    int n;
    cin>>n;

    LL A[n];
    LL B[n];
    REP(i,n)cin>>A[i]>>B[i];
    LL sumA=0;
    REP(i,n)sumA+=A[i];
    REP(i,n)B[i]+=A[i];

    vector<LL> vecA;
    vector<LL> vecB;
    int half=n/2;

    REP(i,1<<half){
        LL num=0;
        REP(j,half){
            if(i>>j & 1){
                num+=B[j];
            }
        }
        vecA.push_back(num);
    }

    REP(i,1<<(n-half)){
        LL num=0;
        REP(j,n-half){
            if(i>>j & 1){
                num+=B[j+half];
            }
        }
        vecB.push_back(num);
    }

    sort(ALL(vecA));
    sort(ALL(vecB));


    LL minimum = 10000000000000000L;

    REP(i,vecA.size()){
        LL va = vecA[i];
        vector<LL>::iterator itr = lower_bound(vecB.begin(),vecB.end(),sumA-va);
        if(itr!=vecB.end()){
            minimum=min(minimum,abs(sumA-(va + *itr)));
        }
        itr--;
        LL ans2=va+ *itr;
        minimum = min(minimum,abs(sumA-ans2));
    }

    cout<<minimum<<endl;


    return 0;
}
0