結果

問題 No.1199 お菓子配り-2
ユーザー shojin_proshojin_pro
提出日時 2020-08-28 22:12:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 557 ms / 1,000 ms
コード長 3,296 bytes
コンパイル時間 2,856 ms
コンパイル使用メモリ 78,356 KB
実行使用メモリ 58,664 KB
最終ジャッジ日時 2024-04-26 14:57:16
合計ジャッジ時間 24,395 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
37,324 KB
testcase_01 AC 50 ms
37,308 KB
testcase_02 AC 50 ms
37,376 KB
testcase_03 AC 284 ms
48,068 KB
testcase_04 AC 455 ms
55,596 KB
testcase_05 AC 124 ms
40,660 KB
testcase_06 AC 145 ms
41,952 KB
testcase_07 AC 323 ms
47,888 KB
testcase_08 AC 377 ms
49,504 KB
testcase_09 AC 299 ms
47,240 KB
testcase_10 AC 180 ms
44,840 KB
testcase_11 AC 265 ms
46,232 KB
testcase_12 AC 260 ms
45,632 KB
testcase_13 AC 170 ms
43,392 KB
testcase_14 AC 170 ms
45,052 KB
testcase_15 AC 155 ms
43,028 KB
testcase_16 AC 346 ms
49,736 KB
testcase_17 AC 298 ms
47,340 KB
testcase_18 AC 403 ms
55,460 KB
testcase_19 AC 213 ms
46,912 KB
testcase_20 AC 461 ms
54,668 KB
testcase_21 AC 424 ms
56,436 KB
testcase_22 AC 369 ms
53,820 KB
testcase_23 AC 390 ms
54,660 KB
testcase_24 AC 431 ms
55,500 KB
testcase_25 AC 181 ms
44,188 KB
testcase_26 AC 423 ms
50,912 KB
testcase_27 AC 361 ms
49,816 KB
testcase_28 AC 514 ms
56,704 KB
testcase_29 AC 547 ms
57,960 KB
testcase_30 AC 533 ms
57,144 KB
testcase_31 AC 537 ms
57,724 KB
testcase_32 AC 513 ms
57,088 KB
testcase_33 AC 545 ms
58,076 KB
testcase_34 AC 516 ms
57,716 KB
testcase_35 AC 553 ms
57,496 KB
testcase_36 AC 522 ms
57,760 KB
testcase_37 AC 530 ms
56,952 KB
testcase_38 AC 512 ms
57,880 KB
testcase_39 AC 553 ms
57,252 KB
testcase_40 AC 512 ms
57,768 KB
testcase_41 AC 518 ms
58,664 KB
testcase_42 AC 522 ms
57,256 KB
testcase_43 AC 549 ms
57,996 KB
testcase_44 AC 535 ms
57,088 KB
testcase_45 AC 554 ms
57,544 KB
testcase_46 AC 557 ms
57,244 KB
testcase_47 AC 518 ms
56,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int n = sc.nextInt();
        int m = sc.nextInt();
        long[][] dp = new long[n+1][n+1];
        for(int i = 0; i < n; i++){
            Arrays.fill(dp[i],-Long.MAX_VALUE/2);
        }
        dp[0][0] = 0;
        for(int i = 0; i < n; i++){
            long total = 0;
            for(int j = 0; j < m; j++){
                total += sc.nextLong();
            }
            for(int j = 0; j <= i; j++){
                dp[i+1][j] = Math.max(dp[i+1][j],dp[i][j]);
                if(j % 2 == 0){
                    dp[i+1][j+1] = Math.max(dp[i+1][j+1],dp[i][j] + total);
                }else{
                    dp[i+1][j+1] = Math.max(dp[i+1][j+1],dp[i][j] - total);
                }
            }
        }
        long ans = 0;
        for(int i = 0; i <= n; i++){
            ans = Math.max(ans,dp[n][i]);
        }
        pw.println(ans);
        pw.flush();
    }

    static class GeekInteger {
        public static void save_sort(int[] array) {
            shuffle(array);
            Arrays.sort(array);
        }
 
        public static int[] shuffle(int[] array) {
            int n = array.length;
            Random random = new Random();
            for (int i = 0, j; i < n; i++) {
                j = i + random.nextInt(n - i);
                int randomElement = array[j];
                array[j] = array[i];
                array[i] = randomElement;
            }
            return array;
        }
 
    }
}

class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;
    public FastScanner(InputStream in) {
        reader = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

    public String next() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken("\n");
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public double nextDouble() {
         return Double.parseDouble(next());
    }
    
    public String[] nextArray(int n) {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = next();
        return a;
    }

    public int[] nextIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public long[] nextLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    } 
}
0