結果

問題 No.545 ママの大事な二人の子供
ユーザー uafr_csuafr_cs
提出日時 2017-10-30 21:37:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 2,083 bytes
コンパイル時間 2,264 ms
コンパイル使用メモリ 77,332 KB
実行使用メモリ 54,996 KB
最終ジャッジ日時 2023-08-14 13:22:29
合計ジャッジ時間 10,852 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
39,212 KB
testcase_01 AC 119 ms
39,848 KB
testcase_02 AC 119 ms
39,188 KB
testcase_03 AC 120 ms
39,432 KB
testcase_04 AC 117 ms
39,200 KB
testcase_05 AC 118 ms
39,832 KB
testcase_06 AC 118 ms
39,420 KB
testcase_07 AC 120 ms
39,384 KB
testcase_08 AC 118 ms
39,260 KB
testcase_09 AC 120 ms
39,848 KB
testcase_10 AC 120 ms
39,732 KB
testcase_11 AC 121 ms
39,552 KB
testcase_12 AC 125 ms
40,120 KB
testcase_13 AC 129 ms
39,776 KB
testcase_14 AC 127 ms
39,224 KB
testcase_15 AC 120 ms
39,548 KB
testcase_16 AC 129 ms
39,948 KB
testcase_17 AC 133 ms
39,436 KB
testcase_18 AC 170 ms
39,988 KB
testcase_19 AC 184 ms
41,040 KB
testcase_20 AC 204 ms
41,756 KB
testcase_21 AC 116 ms
39,344 KB
testcase_22 AC 325 ms
46,596 KB
testcase_23 AC 405 ms
54,512 KB
testcase_24 AC 309 ms
46,384 KB
testcase_25 AC 392 ms
54,220 KB
testcase_26 AC 359 ms
52,200 KB
testcase_27 AC 439 ms
54,612 KB
testcase_28 AC 412 ms
54,552 KB
testcase_29 AC 402 ms
54,452 KB
testcase_30 AC 411 ms
54,996 KB
testcase_31 AC 411 ms
54,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
	
	public static ArrayList<Long> all(int begin, int end, long[] as, long[] bs){
		TreeSet<Long> set = new TreeSet<Long>();
		
		final int size = end - begin;
		final int bit_size = 1 << size;
		
		for(int bit = 0; bit < bit_size; bit++){
			long sum = 0;
			
			for(int i = 0; i < size; i++){
				final int index = i + begin;
				
				if((bit & (1 << i)) == 0){
					sum += as[index];
				}else{
					sum -= bs[index];
				}
			}
			
			set.add(sum);
		}
		
		return new ArrayList<Long>(set);
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int n = sc.nextInt();
		long[] as = new long[n];
		long[] bs = new long[n];
		for(int i = 0; i < n; i++){
			as[i] = sc.nextLong();
			bs[i] = sc.nextLong();
		}
		
		if(n == 1){
			System.out.println(Math.min(as[0], bs[0]));
			return;
		}
		
		final int fst_end = n / 2;
		ArrayList<Long> fst_part = all(0, fst_end, as, bs);
		ArrayList<Long> snd_part = all(fst_end, n, as, bs);
		
		//System.out.println(fst_part);
		//System.out.println(snd_part);
		
		long answer = Long.MAX_VALUE;
		for(int fst_index = 0; fst_index < fst_part.size(); fst_index++){
			final long snd = - fst_part.get(fst_index);
			
			final int snd_index = Collections.binarySearch(snd_part, snd);
			if(snd_index >= 0){
				System.out.println(0);
				return;
			}else{
				final int lower = -(snd_index + 2);
				
				if(lower >= 0){
					//System.out.println(fst_part.get(fst_index) + " " + snd_part.get(lower));
					answer = Math.min(answer, Math.abs(snd_part.get(lower)+ fst_part.get(fst_index)));
				}
				
				final int upper = -(snd_index + 1);
				if(upper < snd_part.size()){
					//System.out.println(fst_part.get(fst_index) + " " + snd_part.get(upper));
					answer = Math.min(answer, Math.abs(snd_part.get(upper) + fst_part.get(fst_index)));
				}
			}
		}
		
		System.out.println(answer);
	}
}
0