結果
問題 | No.1199 お菓子配り-2 |
ユーザー |
![]() |
提出日時 | 2020-08-29 10:16:00 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 593 ms / 1,000 ms |
コード長 | 1,355 bytes |
コンパイル時間 | 2,383 ms |
コンパイル使用メモリ | 78,360 KB |
実行使用メモリ | 61,096 KB |
最終ジャッジ日時 | 2024-11-14 05:23:35 |
合計ジャッジ時間 | 25,468 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 45 |
ソースコード
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];}}