-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.java
More file actions
28 lines (23 loc) · 829 Bytes
/
Copy pathcode.java
File metadata and controls
28 lines (23 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] p = new long[n + 1];
long[] dp = new long[n + 1];
for (int i = 1; i <= n; i++) {
p[i] = sc.nextLong();
}
dp[2] = p[1] - p[2];
// Calculate minimum differences using dynamic programming
for (int i = 3; i <= n; i++) {
for (int j = i - 2; j >= 0; j--) {
long difference = dp[j] + p[j + 1] - p[i];
dp[i] = Math.min(dp[i], difference);
}
}
// Output the result
System.out.println(dp[n]);
sc.close(); // Close the scanner to prevent resource leaks
}
}