結果

問題 No.1117 数列分割
ユーザー tentententen
提出日時 2021-05-10 18:03:39
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,780 bytes
コンパイル時間 2,297 ms
コンパイル使用メモリ 78,456 KB
実行使用メモリ 84,944 KB
最終ジャッジ日時 2023-10-20 02:03:08
合計ジャッジ時間 12,171 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,460 KB
testcase_01 AC 57 ms
52,476 KB
testcase_02 AC 58 ms
52,380 KB
testcase_03 AC 182 ms
60,200 KB
testcase_04 AC 635 ms
60,196 KB
testcase_05 AC 57 ms
53,476 KB
testcase_06 AC 66 ms
53,572 KB
testcase_07 AC 84 ms
54,228 KB
testcase_08 AC 1,351 ms
60,292 KB
testcase_09 AC 241 ms
55,744 KB
testcase_10 AC 1,842 ms
60,468 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
}
0