結果

問題 No.838 Noelちゃんと星々3
ユーザー tentententen
提出日時 2020-12-04 12:35:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 717 bytes
コンパイル時間 4,150 ms
コンパイル使用メモリ 74,392 KB
実行使用メモリ 66,456 KB
最終ジャッジ日時 2023-10-13 00:35:06
合計ジャッジ時間 13,494 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 680 ms
64,620 KB
testcase_01 AC 692 ms
64,736 KB
testcase_02 AC 660 ms
64,952 KB
testcase_03 AC 687 ms
65,236 KB
testcase_04 AC 682 ms
66,456 KB
testcase_05 AC 139 ms
55,920 KB
testcase_06 AC 135 ms
55,720 KB
testcase_07 AC 139 ms
55,972 KB
testcase_08 AC 144 ms
55,880 KB
testcase_09 AC 147 ms
55,944 KB
testcase_10 AC 135 ms
55,976 KB
testcase_11 AC 142 ms
55,716 KB
testcase_12 AC 142 ms
55,724 KB
testcase_13 AC 139 ms
55,836 KB
testcase_14 AC 133 ms
55,476 KB
testcase_15 AC 139 ms
55,768 KB
testcase_16 AC 128 ms
55,808 KB
testcase_17 AC 133 ms
55,768 KB
testcase_18 AC 140 ms
55,740 KB
testcase_19 AC 138 ms
55,908 KB
testcase_20 AC 128 ms
55,908 KB
testcase_21 AC 128 ms
55,544 KB
testcase_22 AC 130 ms
55,764 KB
testcase_23 AC 130 ms
55,428 KB
testcase_24 AC 132 ms
55,472 KB
testcase_25 AC 136 ms
55,800 KB
testcase_26 AC 134 ms
55,796 KB
testcase_27 AC 130 ms
56,080 KB
testcase_28 AC 130 ms
55,784 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);
        long[][] dp = new long[n][2];
        dp[0][1] = 0;
        dp[0][0] = Long.MAX_VALUE / 10;
        dp[1][0] = arr[1] - arr[0];
        dp[1][1] = Long.MAX_VALUE / 10;
        for (int i = 2; i < n; i++) {
            dp[i][0] = Math.min(dp[i - 1][1] + arr[i] - arr[i - 1], dp[i - 2][1] + arr[i] - arr[i - 2]);
            dp[i][1] = dp[i - 1][0];
        }
        System.out.println(dp[n - 1][0]);
    }
}
0