結果

問題 No.609 Noelちゃんと星々
ユーザー jp_stejp_ste
提出日時 2019-08-22 21:44:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 709 ms / 2,000 ms
コード長 661 bytes
コンパイル時間 2,207 ms
コンパイル使用メモリ 74,360 KB
実行使用メモリ 52,200 KB
最終ジャッジ日時 2024-04-21 00:42:10
合計ジャッジ時間 17,032 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 550 ms
48,692 KB
testcase_01 AC 469 ms
48,396 KB
testcase_02 AC 385 ms
48,280 KB
testcase_03 AC 420 ms
47,844 KB
testcase_04 AC 343 ms
46,960 KB
testcase_05 AC 589 ms
48,988 KB
testcase_06 AC 553 ms
48,624 KB
testcase_07 AC 506 ms
48,536 KB
testcase_08 AC 268 ms
46,704 KB
testcase_09 AC 414 ms
48,368 KB
testcase_10 AC 574 ms
48,620 KB
testcase_11 AC 452 ms
48,260 KB
testcase_12 AC 565 ms
48,744 KB
testcase_13 AC 280 ms
47,828 KB
testcase_14 AC 228 ms
46,944 KB
testcase_15 AC 585 ms
48,884 KB
testcase_16 AC 709 ms
48,868 KB
testcase_17 AC 579 ms
48,660 KB
testcase_18 AC 381 ms
47,016 KB
testcase_19 AC 578 ms
48,632 KB
testcase_20 AC 625 ms
52,200 KB
testcase_21 AC 628 ms
49,096 KB
testcase_22 AC 614 ms
49,352 KB
testcase_23 AC 605 ms
50,684 KB
testcase_24 AC 625 ms
49,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner scan = new Scanner(System.in)) {
			int n = scan.nextInt();
			long[] y = new long[n];
			for(int i=0; i<n; i++) {
				y[i] = scan.nextLong();
			}
			Arrays.sort(y);
			long sum = 0;
			if(n % 2 == 1) {
			    sum = calcSum(y, n/2);
			} else {
			    sum = Math.min(calcSum(y, n/2) , calcSum(y, n/2-1));
			}
			System.out.println(sum);
		}
	}
	
	private static long calcSum(long[] y, int index) {
		long sum = 0;
		long base = y[index];
		for(int i=0; i<y.length; i++) {
		    sum += Math.abs(y[i] - base);
		}
		return sum;
	}
}
0