結果

問題 No.838 Noelちゃんと星々3
ユーザー htensaihtensai
提出日時 2020-05-08 17:05:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 632 ms / 2,000 ms
コード長 615 bytes
コンパイル時間 2,473 ms
コンパイル使用メモリ 71,264 KB
実行使用メモリ 62,732 KB
最終ジャッジ日時 2023-09-16 08:31:14
合計ジャッジ時間 9,645 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 632 ms
61,404 KB
testcase_01 AC 605 ms
61,628 KB
testcase_02 AC 607 ms
62,432 KB
testcase_03 AC 607 ms
62,208 KB
testcase_04 AC 609 ms
62,732 KB
testcase_05 AC 130 ms
55,672 KB
testcase_06 AC 125 ms
55,620 KB
testcase_07 AC 127 ms
55,812 KB
testcase_08 AC 131 ms
55,520 KB
testcase_09 AC 130 ms
56,264 KB
testcase_10 AC 124 ms
55,660 KB
testcase_11 AC 130 ms
55,360 KB
testcase_12 AC 133 ms
55,796 KB
testcase_13 AC 131 ms
55,704 KB
testcase_14 AC 125 ms
55,640 KB
testcase_15 AC 134 ms
56,620 KB
testcase_16 AC 122 ms
55,264 KB
testcase_17 AC 122 ms
55,584 KB
testcase_18 AC 133 ms
55,764 KB
testcase_19 AC 131 ms
55,452 KB
testcase_20 AC 123 ms
55,796 KB
testcase_21 AC 125 ms
55,456 KB
testcase_22 AC 122 ms
55,816 KB
testcase_23 AC 125 ms
55,792 KB
testcase_24 AC 123 ms
56,472 KB
testcase_25 AC 124 ms
55,800 KB
testcase_26 AC 121 ms
55,792 KB
testcase_27 AC 120 ms
55,572 KB
testcase_28 AC 121 ms
53,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
		    arr[i] = sc.nextInt();
		}
		Arrays.sort(arr);
		if (n <= 3) {
		    System.out.println(arr[n - 1] - arr[0]);
		    return;
		}
		long[] dp = new long[n];
		dp[0] = Long.MAX_VALUE / 10;
		dp[1] = arr[1] - arr[0];
		dp[2] = arr[2] - arr[0];
		for (int i = 3; i < n; i++) {
		    dp[i] = Math.min(dp[i - 2] + arr[i] - arr[i - 1], dp[i - 3] + arr[i] - arr[i - 2]);
		}
		System.out.println(dp[n - 1]);
	}
}
0