結果
問題 | No.1117 数列分割 |
ユーザー |
![]() |
提出日時 | 2021-05-10 18:03:39 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,780 bytes |
コンパイル時間 | 2,062 ms |
コンパイル使用メモリ | 77,776 KB |
実行使用メモリ | 52,968 KB |
最終ジャッジ日時 | 2024-09-19 21:51:41 |
合計ジャッジ時間 | 9,885 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 5 TLE * 1 -- * 20 |
ソースコード
import java.io.*; import java.util.*; public class Main { static int m; static long[] sums; static long[][] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int k = sc.nextInt(); m = sc.nextInt(); sums = new long[n + 1]; for (int i = 1; i <= n; i++) { sums[i] = sums[i - 1] + sc.nextInt(); } dp = new long[n + 1][k + 1]; for (long[] arr : dp) { Arrays.fill(arr, Long.MAX_VALUE); } System.out.println(dfw(n, k)); } static long dfw(int idx, int group) { if (idx == 0 && group == 0) { return 0; } if (idx < group) { return Long.MIN_VALUE; } if (idx > group * m) { return Long.MIN_VALUE; } if (dp[idx][group] == Long.MAX_VALUE) { long max = 0; for (int i = idx - 1; i >= 0 && idx - i <= m; i--) { max = Math.max(max, dfw(i, group - 1) + Math.abs(sums[idx] - sums[i])); } dp[idx][group] = max; } return dp[idx][group]; } } 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 String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }