結果

問題 No.545 ママの大事な二人の子供
ユーザー tanzakutanzaku
提出日時 2017-07-14 22:49:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 316 ms / 2,000 ms
コード長 1,493 bytes
コンパイル時間 3,783 ms
コンパイル使用メモリ 78,612 KB
実行使用メモリ 44,148 KB
最終ジャッジ日時 2024-04-16 20:37:15
合計ジャッジ時間 11,117 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
41,408 KB
testcase_01 AC 136 ms
41,696 KB
testcase_02 AC 135 ms
41,652 KB
testcase_03 AC 134 ms
41,288 KB
testcase_04 AC 133 ms
41,168 KB
testcase_05 AC 136 ms
41,416 KB
testcase_06 AC 133 ms
41,608 KB
testcase_07 AC 138 ms
41,428 KB
testcase_08 AC 138 ms
41,456 KB
testcase_09 AC 139 ms
41,464 KB
testcase_10 AC 137 ms
41,620 KB
testcase_11 AC 139 ms
41,728 KB
testcase_12 AC 140 ms
41,444 KB
testcase_13 AC 144 ms
41,648 KB
testcase_14 AC 128 ms
40,464 KB
testcase_15 AC 139 ms
41,272 KB
testcase_16 AC 143 ms
41,288 KB
testcase_17 AC 145 ms
41,432 KB
testcase_18 AC 154 ms
41,904 KB
testcase_19 AC 172 ms
41,528 KB
testcase_20 AC 213 ms
41,800 KB
testcase_21 AC 136 ms
41,272 KB
testcase_22 AC 259 ms
43,220 KB
testcase_23 AC 310 ms
43,924 KB
testcase_24 AC 258 ms
42,644 KB
testcase_25 AC 306 ms
43,840 KB
testcase_26 AC 315 ms
43,792 KB
testcase_27 AC 316 ms
43,796 KB
testcase_28 AC 314 ms
44,092 KB
testcase_29 AC 312 ms
43,948 KB
testcase_30 AC 304 ms
44,148 KB
testcase_31 AC 313 ms
44,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


import java.util.*;

public class No545 {

	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, cur);
					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