|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.StringTokenizer; |
| 5 | +
|
| 6 | +public class Main { |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static final long INF = (long)2e14; |
| 10 | + private static long[] dp; |
| 11 | + private static int[] oranges; |
| 12 | + private static int N, M, K; |
| 13 | +
|
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + init(); |
| 16 | +
|
| 17 | + bw.write(dp[N] + "\n"); |
| 18 | + bw.flush(); |
| 19 | + bw.close(); |
| 20 | + br.close(); |
| 21 | + } |
| 22 | +
|
| 23 | + private static void init() throws IOException { |
| 24 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 25 | + N = Integer.parseInt(st.nextToken()); |
| 26 | + M = Integer.parseInt(st.nextToken()); |
| 27 | + K = Integer.parseInt(st.nextToken()); |
| 28 | +
|
| 29 | + oranges = new int[N+1]; |
| 30 | + dp = new long[N+1]; |
| 31 | + for (int i = 1; i <= N; i++) { |
| 32 | + oranges[i] = Integer.parseInt(br.readLine()); |
| 33 | + } |
| 34 | +
|
| 35 | + Arrays.fill(dp, INF); |
| 36 | + dp[0] = 0; |
| 37 | +
|
| 38 | + for (int i = 1; i <= N; i++) { |
| 39 | + long max = Long.MIN_VALUE; |
| 40 | + long min = Long.MAX_VALUE; |
| 41 | +
|
| 42 | + for (int j = i; j >= Math.max(1, i-M+1); j--) { |
| 43 | + max = Math.max(max, oranges[j]); |
| 44 | + min = Math.min(min, oranges[j]); |
| 45 | + long cost = K+(i-j+1)*(max-min); |
| 46 | + dp[i] = Math.min(dp[i], dp[j-1]+cost); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
0 commit comments