結果

問題 No.545 ママの大事な二人の子供
ユーザー baito_pbaito_p
提出日時 2017-07-15 01:30:50
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,559 bytes
コンパイル時間 2,034 ms
コンパイル使用メモリ 77,964 KB
実行使用メモリ 60,472 KB
最終ジャッジ日時 2024-04-16 20:56:12
合計ジャッジ時間 7,975 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
41,904 KB
testcase_01 AC 51 ms
36,956 KB
testcase_02 WA -
testcase_03 AC 51 ms
36,884 KB
testcase_04 WA -
testcase_05 AC 49 ms
36,724 KB
testcase_06 AC 51 ms
36,788 KB
testcase_07 AC 51 ms
36,572 KB
testcase_08 AC 50 ms
36,196 KB
testcase_09 AC 51 ms
36,804 KB
testcase_10 AC 51 ms
36,508 KB
testcase_11 AC 52 ms
36,332 KB
testcase_12 AC 56 ms
36,596 KB
testcase_13 AC 88 ms
38,188 KB
testcase_14 AC 96 ms
38,980 KB
testcase_15 AC 54 ms
36,852 KB
testcase_16 AC 93 ms
38,472 KB
testcase_17 AC 149 ms
38,836 KB
testcase_18 AC 1,056 ms
38,784 KB
testcase_19 TLE -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
 
public class Main {
    public static void main(String[] args)throws Exception{
        FastScanner sc = new FastScanner();
        int n = sc.nextInt();
        int a[] = new int[n]; 
        int b[] = new int[n]; 
        for(int i = 0 ; i< n ; i++) {
        	a[i] = sc.nextInt();
        	b[i] = sc.nextInt();
        }
        long min = 160000000000L;
        long ans = 0;
        long asum = 0;
        long bsum = 0;
        int cou = 0;
        boolean[] tf = new boolean[n];
        long i;
        for(i= 0 ; i < Math.pow(2.0,(double)n); ) {
        	for(int j = 0 ; j< n ; j++) {
        		if(tf[j]) asum += a[j];
        		else bsum += b[j];
        	}
        	
        i++;
        if(!(i < Math.pow(2.0,(double)n)) )break;
        long k = i ;
       
        for(int q = 0 ; q < n ;q++) {
        	tf[q]= false;
        }
        int l= 0;
        while(k > 0) {
        	
        	if(k %2 == 1)tf[l] = true;
        	//else if(tf[l]==true)tf[l++]= false;
        	l++;
        	k /= 2;
        }
        
        	ans = abs(asum - bsum);
        	if(ans < min) min =ans;
        	asum =0;
        	bsum =0;
        	//System.out.println(cou);
        }
    
        
        System.out.println(min);

    }

	private static long abs(long l) {
		// TODO 自動生成されたメソッド・スタブ
		if(l > 0) return l;
		else return -l;
	}
    
     
}
class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}
0