結果
| 問題 |
No.838 Noelちゃんと星々3
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-11-04 08:23:35 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 291 ms / 2,000 ms |
| コード長 | 1,603 bytes |
| コンパイル時間 | 2,587 ms |
| コンパイル使用メモリ | 77,872 KB |
| 実行使用メモリ | 47,588 KB |
| 最終ジャッジ日時 | 2024-10-14 04:13:07 |
| 合計ジャッジ時間 | 6,746 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
import java.io.*;
import java.util.*;
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);
if (n == 2) {
System.out.println(values[1] - values[0]);
return;
}
long[] dp = new long[n + 1];
dp[0] = 0;
dp[1] = Long.MAX_VALUE / 2;
dp[2] = values[1] - values[0];
dp[3] = values[2] - values[0];
for (int i = 4; 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 Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
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 String nextLine() throws Exception {
return br.readLine();
}
public String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten