結果

問題 No.838 Noelちゃんと星々3
ユーザー 37zigen37zigen
提出日時 2019-06-15 03:25:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 676 ms / 2,000 ms
コード長 784 bytes
コンパイル時間 3,227 ms
コンパイル使用メモリ 74,080 KB
実行使用メモリ 62,568 KB
最終ジャッジ日時 2023-08-10 05:45:39
合計ジャッジ時間 11,622 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 676 ms
62,444 KB
testcase_01 AC 627 ms
62,376 KB
testcase_02 AC 636 ms
62,328 KB
testcase_03 AC 636 ms
62,568 KB
testcase_04 AC 638 ms
62,376 KB
testcase_05 AC 134 ms
55,824 KB
testcase_06 AC 124 ms
56,236 KB
testcase_07 AC 127 ms
56,132 KB
testcase_08 AC 132 ms
55,760 KB
testcase_09 AC 131 ms
55,756 KB
testcase_10 AC 128 ms
55,844 KB
testcase_11 AC 132 ms
55,848 KB
testcase_12 AC 132 ms
56,220 KB
testcase_13 AC 130 ms
55,760 KB
testcase_14 AC 124 ms
56,160 KB
testcase_15 AC 133 ms
55,584 KB
testcase_16 AC 122 ms
55,908 KB
testcase_17 AC 121 ms
56,304 KB
testcase_18 AC 132 ms
56,056 KB
testcase_19 AC 132 ms
55,940 KB
testcase_20 AC 122 ms
55,800 KB
testcase_21 AC 123 ms
56,260 KB
testcase_22 AC 122 ms
55,548 KB
testcase_23 AC 122 ms
55,580 KB
testcase_24 AC 122 ms
55,580 KB
testcase_25 AC 122 ms
55,916 KB
testcase_26 AC 124 ms
55,596 KB
testcase_27 AC 123 ms
56,384 KB
testcase_28 AC 123 ms
55,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] Y = new long[N];
		for (int i = 0; i < N; ++i) {
			Y[i] = sc.nextLong();
		}
		Arrays.sort(Y);

		long[] dp = new long[N];
		Arrays.fill(dp, Long.MAX_VALUE / 3);
		for (int i = 1; i < N; ++i) {
			dp[i] = Math.min(dp[i], (i >= 2 ? dp[i - 2] : 0) + Math.abs(Y[i] - Y[i - 1]));
			if (i >= 2)
				dp[i] = Math.min(dp[i],
						(i >= 3 ? dp[i - 3] : 0) + Math.abs(Y[i] - Y[i - 1]) + Math.abs(Y[i - 1] - Y[i - 2]));
		}
		System.out.println(dp[N - 1]);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0