結果

問題 No.545 ママの大事な二人の子供
ユーザー rapurasurapurasu
提出日時 2017-07-15 13:54:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,429 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 164,704 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 21:46:27
合計ジャッジ時間 2,702 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 18 ms
5,376 KB
testcase_17 AC 18 ms
5,376 KB
testcase_18 AC 17 ms
5,376 KB
testcase_19 AC 17 ms
5,376 KB
testcase_20 AC 18 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 23 ms
5,376 KB
testcase_23 AC 39 ms
5,376 KB
testcase_24 AC 18 ms
5,376 KB
testcase_25 AC 32 ms
5,376 KB
testcase_26 AC 37 ms
5,376 KB
testcase_27 AC 24 ms
5,376 KB
testcase_28 AC 38 ms
5,376 KB
testcase_29 AC 29 ms
5,376 KB
testcase_30 AC 35 ms
5,376 KB
testcase_31 AC 31 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;
LL sum=0;
int n=0;
LL A[40];
LL B[40];
LL C[40];
int main(){
    cin>>n;
    REP(i,n){
        cin>>A[i]>>B[i];
    }
    vector<LL>v;
    vector<LL>v2;
    REP(i,1<<(min(16,n))){
        LL now=0;
        REP(j,min(16,n)){
            if((i>>j)&1){
               now+=A[j];
            }else{
               now-=B[j];
            }
        }
        v.push_back(now);
        v2.push_back(-now);
        //cout<<now<<endl;
    }
    sort(v.begin(),v.end());
    sort(v2.begin(),v2.end());
    if(n<=16){
       LL ans=1e12;
       REP(i,v.size()){
           ans=min(ans,abs(v[i]));
       }
       cout<<ans<<endl;
    }else{
       LL ans=1e12;
       REP(i,1<<(min(16,n-16))){
          LL now=0;
          REP(j,min(16,n-16)){
              if((i>>j)&1){
                 now+=A[j+16];
              }else{
                 now-=B[j+16];
              }
          }
          LL a=v[lower_bound(v.begin(),v.end(),-now)-v.begin()];
          LL b=v2[lower_bound(v2.begin(),v2.end(),(now))-v2.begin()];
          //cout<<now<<" "<<a<<" "<<b<<endl;
          ans=min(ans,abs(a+now));
          ans=min(ans,abs(b-now));
      }
      cout<<ans<<endl;

    }
    
    return(0);
}
0