結果

問題 No.838 Noelちゃんと星々3
ユーザー tentententen
提出日時 2023-11-16 08:53:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,908 bytes
コンパイル時間 2,592 ms
コンパイル使用メモリ 88,520 KB
実行使用メモリ 62,480 KB
最終ジャッジ日時 2023-11-16 08:53:56
合計ジャッジ時間 7,024 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 300 ms
62,480 KB
testcase_01 AC 282 ms
62,124 KB
testcase_02 AC 272 ms
60,184 KB
testcase_03 AC 288 ms
62,100 KB
testcase_04 AC 272 ms
62,136 KB
testcase_05 AC 53 ms
54,124 KB
testcase_06 AC 52 ms
54,008 KB
testcase_07 AC 51 ms
54,120 KB
testcase_08 AC 53 ms
54,120 KB
testcase_09 AC 53 ms
54,088 KB
testcase_10 AC 55 ms
54,004 KB
testcase_11 AC 54 ms
54,128 KB
testcase_12 AC 53 ms
54,080 KB
testcase_13 AC 53 ms
54,124 KB
testcase_14 AC 52 ms
54,080 KB
testcase_15 AC 52 ms
52,040 KB
testcase_16 AC 52 ms
54,004 KB
testcase_17 AC 52 ms
54,112 KB
testcase_18 AC 53 ms
54,012 KB
testcase_19 AC 52 ms
54,008 KB
testcase_20 AC 52 ms
54,084 KB
testcase_21 AC 51 ms
54,100 KB
testcase_22 AC 54 ms
54,076 KB
testcase_23 AC 53 ms
54,016 KB
testcase_24 AC 53 ms
54,076 KB
testcase_25 AC 54 ms
54,012 KB
testcase_26 AC 52 ms
54,000 KB
testcase_27 AC 52 ms
54,068 KB
testcase_28 AC 52 ms
54,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        long[] values = new long[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        Arrays.sort(values);
        long[] dp = new long[n + 1];
        dp[1] = Long.MAX_VALUE / 2;
        dp[2] = values[1] - values[0];
        for (int i = 3; i <= n; i++) {
            dp[i] = Math.min(dp[i - 2] + values[i - 1] - values[i - 2], dp[i - 3] + values[i - 1] - values[i - 3]);
        }
        System.out.println(dp[n]);
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0