結果

問題 No.545 ママの大事な二人の子供
ユーザー tanzakutanzaku
提出日時 2017-07-14 22:47:35
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,494 bytes
コンパイル時間 3,401 ms
コンパイル使用メモリ 78,252 KB
実行使用メモリ 56,052 KB
最終ジャッジ日時 2024-04-16 20:37:02
合計ジャッジ時間 10,045 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 113 ms
41,728 KB
testcase_02 WA -
testcase_03 AC 118 ms
41,460 KB
testcase_04 AC 121 ms
41,916 KB
testcase_05 AC 125 ms
41,672 KB
testcase_06 WA -
testcase_07 AC 130 ms
41,696 KB
testcase_08 WA -
testcase_09 AC 122 ms
41,424 KB
testcase_10 AC 123 ms
41,384 KB
testcase_11 AC 126 ms
41,836 KB
testcase_12 AC 124 ms
41,556 KB
testcase_13 AC 125 ms
41,812 KB
testcase_14 AC 118 ms
41,508 KB
testcase_15 AC 133 ms
41,428 KB
testcase_16 AC 129 ms
41,860 KB
testcase_17 AC 117 ms
41,692 KB
testcase_18 AC 139 ms
41,844 KB
testcase_19 AC 155 ms
41,572 KB
testcase_20 AC 191 ms
42,688 KB
testcase_21 AC 122 ms
41,128 KB
testcase_22 WA -
testcase_23 AC 294 ms
44,460 KB
testcase_24 AC 243 ms
43,108 KB
testcase_25 AC 291 ms
44,216 KB
testcase_26 WA -
testcase_27 AC 291 ms
43,736 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 305 ms
43,888 KB
testcase_31 AC 294 ms
44,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


import java.util.*;

public class No546 {

	public static void main(String[] args) {
		try (final Scanner sc = new Scanner(System.in)) {
			int n = sc.nextInt();
			long[] A = new long[n];
			long[] B = new long[n];
			for (int i = 0; i < n; i++) {
				A[i] = sc.nextLong();
				B[i] = sc.nextLong();
			}
			if (n == 1) {
				System.out.println(Math.min(A[0], B[0]));
				return;
			}
			long[] s1 = new long[1<<(n/2)];
			long[] s2 = new long[1<<((n+1)/2)];
			for (int i = 0; i < 1<<(n/2); i++) {
				long s = 0;
				for (int j = 0; j < n/2; j++) {
					if ((i>>j&1) == 1) {
						s += A[j];
					} else {
						s -= B[j];
					}
				}
				s1[i] = s;
			}
			for (int i = 0; i < 1<<((n+1)/2); i++) {
				long s = 0;
				for (int j = 0; j < (n+1)/2; j++) {
					if ((i>>j&1) == 1) {
						s += A[n/2+j];
					} else {
						s -= B[n/2+j];
					}
				}
				s2[i] = s;
			}
			Arrays.sort(s1);
			Arrays.sort(s2);
			long ans = Long.MAX_VALUE;
			for (int i = 0, j = s2.length - 1; i < s1.length; i++) {
				for (; j > 0; j--) {
					long cur = Math.abs(s1[i] + s2[j]);
					long next = Math.abs(s1[i] + s2[j-1]);
					ans = Math.min(ans, next);
					ans = Math.min(ans, next);
					if (cur < next) {
						break;
					}
				}
			}
			System.out.println(ans);
		}
	}
	
	static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n % r); }
	static long lcm(long n, long r) { return n/gcd(n,r)*r; }
	static void dump(Object... objects) { System.err.println(Arrays.deepToString(objects)); }
}
0