結果

問題 No.1199 お菓子配り-2
ユーザー tentententen
提出日時 2023-08-02 08:51:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 531 ms / 1,000 ms
コード長 2,288 bytes
コンパイル時間 2,887 ms
コンパイル使用メモリ 84,976 KB
実行使用メモリ 48,520 KB
最終ジャッジ日時 2024-04-20 08:25:06
合計ジャッジ時間 28,514 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
36,820 KB
testcase_01 AC 55 ms
37,520 KB
testcase_02 AC 55 ms
37,576 KB
testcase_03 AC 333 ms
45,200 KB
testcase_04 AC 460 ms
47,392 KB
testcase_05 AC 118 ms
40,872 KB
testcase_06 AC 154 ms
42,824 KB
testcase_07 AC 326 ms
46,412 KB
testcase_08 AC 384 ms
46,368 KB
testcase_09 AC 348 ms
46,208 KB
testcase_10 AC 183 ms
45,236 KB
testcase_11 AC 274 ms
45,480 KB
testcase_12 AC 284 ms
45,256 KB
testcase_13 AC 182 ms
44,684 KB
testcase_14 AC 173 ms
44,024 KB
testcase_15 AC 164 ms
43,120 KB
testcase_16 AC 366 ms
46,544 KB
testcase_17 AC 313 ms
46,176 KB
testcase_18 AC 392 ms
47,476 KB
testcase_19 AC 229 ms
45,116 KB
testcase_20 AC 456 ms
46,324 KB
testcase_21 AC 420 ms
46,256 KB
testcase_22 AC 333 ms
46,472 KB
testcase_23 AC 383 ms
46,552 KB
testcase_24 AC 432 ms
46,632 KB
testcase_25 AC 189 ms
43,428 KB
testcase_26 AC 427 ms
47,756 KB
testcase_27 AC 392 ms
47,604 KB
testcase_28 AC 489 ms
46,472 KB
testcase_29 AC 492 ms
47,812 KB
testcase_30 AC 489 ms
48,188 KB
testcase_31 AC 531 ms
47,744 KB
testcase_32 AC 483 ms
47,648 KB
testcase_33 AC 482 ms
46,436 KB
testcase_34 AC 478 ms
46,220 KB
testcase_35 AC 483 ms
46,156 KB
testcase_36 AC 506 ms
46,252 KB
testcase_37 AC 484 ms
48,036 KB
testcase_38 AC 491 ms
46,236 KB
testcase_39 AC 527 ms
48,520 KB
testcase_40 AC 491 ms
47,948 KB
testcase_41 AC 500 ms
46,264 KB
testcase_42 AC 531 ms
47,940 KB
testcase_43 AC 490 ms
46,040 KB
testcase_44 AC 493 ms
46,564 KB
testcase_45 AC 488 ms
46,340 KB
testcase_46 AC 496 ms
46,392 KB
testcase_47 AC 526 ms
47,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static long[] values;
    static long[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        values = new long[n];
        for (int i = n - 1; i >= 0; i--) {
            for (int j = 0; j < m; j++) {
                values[i] += sc.nextLong();
            }
        }
        dp = new long[n][2];
        for (long[] arr : dp) {
            Arrays.fill(arr, Long.MIN_VALUE);
        }
        System.out.println(dfw(n - 1, 0));
    }
    
    static long dfw(int idx, int v) {
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][v] == Long.MIN_VALUE) {
            if (v == 0) {
                dp[idx][v] = Math.max(dfw(idx - 1, v), dfw(idx - 1, 1) + values[idx]);
            } else {
                dp[idx][v] = Math.max(dfw(idx - 1, v), dfw(idx - 1, 0) - values[idx]);
            }
        }
        return dp[idx][v];
    }
} 
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0