結果

問題 No.1199 お菓子配り-2
ユーザー tentententen
提出日時 2020-08-29 10:16:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 565 ms / 1,000 ms
コード長 1,355 bytes
コンパイル時間 2,584 ms
コンパイル使用メモリ 77,716 KB
実行使用メモリ 48,768 KB
最終ジャッジ日時 2024-04-26 16:11:18
合計ジャッジ時間 26,602 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
36,728 KB
testcase_01 AC 46 ms
36,784 KB
testcase_02 AC 46 ms
36,580 KB
testcase_03 AC 350 ms
46,504 KB
testcase_04 AC 424 ms
47,000 KB
testcase_05 AC 109 ms
40,560 KB
testcase_06 AC 131 ms
42,332 KB
testcase_07 AC 297 ms
46,944 KB
testcase_08 AC 341 ms
46,508 KB
testcase_09 AC 287 ms
46,456 KB
testcase_10 AC 172 ms
43,552 KB
testcase_11 AC 260 ms
44,920 KB
testcase_12 AC 264 ms
44,212 KB
testcase_13 AC 167 ms
43,388 KB
testcase_14 AC 158 ms
43,444 KB
testcase_15 AC 149 ms
43,628 KB
testcase_16 AC 317 ms
46,044 KB
testcase_17 AC 261 ms
45,012 KB
testcase_18 AC 347 ms
45,972 KB
testcase_19 AC 214 ms
44,860 KB
testcase_20 AC 416 ms
47,140 KB
testcase_21 AC 413 ms
46,732 KB
testcase_22 AC 295 ms
45,188 KB
testcase_23 AC 327 ms
46,436 KB
testcase_24 AC 379 ms
46,520 KB
testcase_25 AC 183 ms
45,064 KB
testcase_26 AC 370 ms
46,404 KB
testcase_27 AC 350 ms
46,524 KB
testcase_28 AC 516 ms
47,268 KB
testcase_29 AC 535 ms
47,312 KB
testcase_30 AC 553 ms
47,932 KB
testcase_31 AC 538 ms
46,920 KB
testcase_32 AC 552 ms
47,508 KB
testcase_33 AC 557 ms
47,120 KB
testcase_34 AC 541 ms
47,012 KB
testcase_35 AC 516 ms
47,400 KB
testcase_36 AC 565 ms
47,748 KB
testcase_37 AC 508 ms
47,708 KB
testcase_38 AC 494 ms
47,656 KB
testcase_39 AC 480 ms
47,188 KB
testcase_40 AC 521 ms
47,600 KB
testcase_41 AC 507 ms
47,224 KB
testcase_42 AC 515 ms
47,436 KB
testcase_43 AC 492 ms
47,636 KB
testcase_44 AC 561 ms
47,552 KB
testcase_45 AC 504 ms
47,068 KB
testcase_46 AC 535 ms
47,212 KB
testcase_47 AC 522 ms
48,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static long[] snacks;
    static long[][] dp;
	public static void main (String[] args) throws Exception{
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String[] first = br.readLine().split(" ", 2);
    	int n = Integer.parseInt(first[0]);
     	int m = Integer.parseInt(first[1]);
     	snacks = new long[n];
     	for (int i = 0; i < n; i++) {
     	    String[] line = br.readLine().split(" ", m);
     	    for (int j = 0; j < m; j++) {
     	        snacks[i] += Long.parseLong(line[j]);
     	    }
     	}
     	dp = new long[n][2];
     	for (long[] arr : dp) {
     	    Arrays.fill(arr, Long.MIN_VALUE / 10);
     	}
     	System.out.println(Math.max(dfw(n - 1, 0), dfw(n - 1, 1)));
    }
    
    static long dfw(int idx, int evod) {
        if (idx < 0) {
            if (evod == 0) {
                return 0;
            } else {
                return Long.MIN_VALUE / 10;
            }
        }
        if (dp[idx][evod] == Long.MIN_VALUE / 10) {
            if (evod == 0) {
                dp[idx][evod] = Math.max(dfw(idx - 1, 0), dfw(idx - 1, 1) - snacks[idx]);
            } else {
                dp[idx][evod] = Math.max(dfw(idx - 1, 1), dfw(idx - 1, 0) + snacks[idx]);
            }
        }
        return dp[idx][evod];
    }
}
0