結果
| 問題 | No.545 ママの大事な二人の子供 | 
| コンテスト | |
| ユーザー |  ikd | 
| 提出日時 | 2017-11-24 20:46:17 | 
| 言語 | D (dmd 2.109.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 39 ms / 2,000 ms | 
| コード長 | 1,030 bytes | 
| コンパイル時間 | 756 ms | 
| コンパイル使用メモリ | 109,652 KB | 
| 実行使用メモリ | 6,948 KB | 
| 最終ジャッジ日時 | 2024-06-12 22:37:14 | 
| 合計ジャッジ時間 | 2,004 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 32 | 
ソースコード
void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.math, std.range;
  int n; rd(n);
  auto a=new long[](n), b=new long[](n);
  foreach(i; 0..n) rd(a[i], b[i]);
  auto data=new long[](0);
  foreach(bit; 0..(1<<(n/2))){
    long sa=0, sb=0;
    foreach(i; 0..(n/2)){
      if(bit&(1<<i)) sa+=a[i];
      else sb+=b[i];
    }
    data~=(sa-sb);
  }
  sort(data);
  auto p=assumeSorted(data);
  auto m=n-n/2;
  long mn=1_000_000_000_000_000_000;
  foreach(bit; 0..(1<<m)){
    long sa=0, sb=0;
    foreach(i; 0..m){
      if(bit&(1<<i)) sa+=a[i+n/2];
      else sb+=b[i+n/2];
    }
    auto d=sa-sb;
    auto lb=p.lowerBound(-d),
         ub=p.upperBound(-d);
    if(lb.length+ub.length<p.length) mn=0;
    if(lb.length>0) mn=min(mn, (d+lb.back).abs);
    if(ub.length>0) mn=min(mn, (d+ub.front).abs);
  }
  writeln(mn);
}
void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
            
            
            
        