結果

問題 No.1374 Absolute Game
ユーザー ks2mks2m
提出日時 2021-02-05 21:42:42
言語 Java19
(openjdk 21)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 1,210 bytes
コンパイル時間 4,231 ms
コンパイル使用メモリ 74,048 KB
実行使用メモリ 61,924 KB
最終ジャッジ日時 2023-09-15 07:32:16
合計ジャッジ時間 8,230 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,256 KB
testcase_01 AC 42 ms
49,364 KB
testcase_02 AC 43 ms
49,576 KB
testcase_03 AC 43 ms
49,252 KB
testcase_04 AC 43 ms
49,208 KB
testcase_05 AC 44 ms
49,344 KB
testcase_06 AC 44 ms
49,280 KB
testcase_07 AC 45 ms
49,204 KB
testcase_08 AC 164 ms
56,072 KB
testcase_09 AC 250 ms
61,088 KB
testcase_10 AC 256 ms
61,420 KB
testcase_11 AC 216 ms
56,576 KB
testcase_12 AC 158 ms
58,136 KB
testcase_13 AC 249 ms
60,988 KB
testcase_14 AC 273 ms
61,336 KB
testcase_15 AC 262 ms
60,856 KB
testcase_16 AC 275 ms
61,556 KB
testcase_17 AC 265 ms
61,744 KB
testcase_18 AC 276 ms
60,992 KB
testcase_19 AC 269 ms
61,924 KB
testcase_20 AC 180 ms
56,856 KB
testcase_21 AC 208 ms
56,300 KB
testcase_22 AC 280 ms
61,784 KB
testcase_23 AC 118 ms
53,164 KB
testcase_24 AC 127 ms
55,152 KB
testcase_25 AC 135 ms
54,904 KB
testcase_26 AC 166 ms
57,904 KB
testcase_27 AC 202 ms
58,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] c = new int[n];
		for (int i = 0; i < n; i++) {
			c[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		Arrays.sort(c);
		long ans1 = calc(c);
		reverse(c);
		long ans2 = calc(c);
		System.out.println(Math.max(ans1, ans2));
	}

	static long calc(int[] c) {
		int n = c.length;
		long suma = 0;
		long sumb = 0;
		for (int i = 0; i < n; i++) {
			if (i % 2 == 0) {
				suma += c[i];
			} else {
				sumb += c[i];
			}
		}
		long val1 = Math.abs(suma) - Math.abs(sumb);

		int n2 = (n + 1) / 2;
		suma = 0;
		sumb = 0;
		for (int i = 0; i < n2; i++) {
			suma += c[i];
		}
		for (int i = n2; i < n; i++) {
			sumb += c[i];
		}
		long val2 = Math.abs(suma) - Math.abs(sumb);
		return Math.min(val1, val2);
	}

	static void reverse(int[] a) {
		for (int i = 0; i < a.length / 2; i++) {
			int tmp = a[i];
			a[i] = a[a.length - 1 - i];
			a[a.length - 1 - i] = tmp;
		}
	}
}
0