結果
| 問題 | No.545 ママの大事な二人の子供 | 
| コンテスト | |
| ユーザー |  rapurasu | 
| 提出日時 | 2017-07-15 13:54:28 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 39 ms / 2,000 ms | 
| コード長 | 1,429 bytes | 
| コンパイル時間 | 1,935 ms | 
| コンパイル使用メモリ | 164,456 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-08 02:22:00 | 
| 合計ジャッジ時間 | 3,066 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 32 | 
ソースコード
#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);
}
            
            
            
        