結果
問題 |
No.545 ママの大事な二人の子供
|
ユーザー |
![]() |
提出日時 | 2017-07-15 00:02:33 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,289 bytes |
コンパイル時間 | 2,105 ms |
コンパイル使用メモリ | 78,348 KB |
実行使用メモリ | 85,168 KB |
最終ジャッジ日時 | 2024-10-08 00:14:36 |
合計ジャッジ時間 | 11,943 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 9 WA * 13 TLE * 2 -- * 8 |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) { 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(); } int med = n / 2; ArrayList<Long> list1 = new ArrayList<Long>(); for(int i = 0; i < (int)Math.pow(2, med); i++) { long t = 0; for(int j = 0; j <= med; j++) { if((i & (1 << j)) != 0) { t += a[j]; } else { t -= b[j]; } } list1.add(t); } ArrayList<Long> list2 = new ArrayList<Long>(); if(n - 1 - med >= 0) { for(int i = 0; i < (int)Math.pow(2, n - 1 - med); i++) { long t = 0; for(int j = 0; j < n - 1 - med; j++) { if((i & (1 << j)) != 0) { t += a[j + med + 1]; } else { t -= b[j + med + 1]; } } list2.add(t); } } else { list2.add((long)0); } long ans = (long)Math.pow(10, 15); for(int i = 0; i < list1.size(); i++) { for(int j = 0; j < list2.size(); j++) { long v = Math.abs(list1.get(i) + list2.get(j)); ans = Math.min(ans, v); } } System.out.println(ans); } }