結果

問題 No.324 落ちてた閉路グラフ
ユーザー GrenacheGrenache
提出日時 2015-12-18 16:06:30
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 3,261 bytes
コンパイル時間 4,198 ms
コンパイル使用メモリ 75,724 KB
実行使用メモリ 537,448 KB
最終ジャッジ日時 2023-10-14 13:59:25
合計ジャッジ時間 24,582 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
49,640 KB
testcase_01 AC 47 ms
49,188 KB
testcase_02 AC 48 ms
49,076 KB
testcase_03 AC 47 ms
49,340 KB
testcase_04 AC 1,156 ms
244,200 KB
testcase_05 MLE -
testcase_06 AC 1,057 ms
233,912 KB
testcase_07 AC 2,400 ms
494,692 KB
testcase_08 AC 1,085 ms
234,796 KB
testcase_09 AC 506 ms
129,708 KB
testcase_10 AC 1,080 ms
234,764 KB
testcase_11 AC 503 ms
130,972 KB
testcase_12 RE -
testcase_13 AC 60 ms
50,480 KB
testcase_14 AC 276 ms
88,492 KB
testcase_15 AC 515 ms
131,864 KB
testcase_16 RE -
testcase_17 AC 46 ms
49,324 KB
testcase_18 AC 47 ms
49,308 KB
testcase_19 AC 46 ms
49,384 KB
testcase_20 RE -
testcase_21 AC 47 ms
47,324 KB
testcase_22 AC 48 ms
49,296 KB
testcase_23 AC 47 ms
49,248 KB
testcase_24 RE -
testcase_25 AC 46 ms
49,188 KB
testcase_26 AC 45 ms
49,304 KB
testcase_27 AC 46 ms
49,224 KB
testcase_28 AC 47 ms
49,060 KB
testcase_29 RE -
testcase_30 AC 45 ms
49,348 KB
testcase_31 AC 46 ms
49,392 KB
testcase_32 MLE -
testcase_33 AC 2,279 ms
466,752 KB
testcase_34 AC 1,093 ms
235,652 KB
testcase_35 AC 62 ms
50,332 KB
testcase_36 AC 287 ms
87,912 KB
testcase_37 AC 49 ms
49,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;


public class Main_yukicoder324_1 {

	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        final int INF = Integer.MAX_VALUE / 2;

        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] w = new int[n];
        for (int i = 0; i < n; i++) {
        	w[i] = sc.nextInt();
        }

        int[][][] dp = new int[n][m + 1][2];
        int[][][] dp2 = new int[n][m + 1][2];
        for (int i = 0; i < n; i++) {
        	for (int j = 0; j <= m; j++) {
        		Arrays.fill(dp[i][j], -INF);
        		Arrays.fill(dp2[i][j], -INF);
        	}
        }
        for (int i = 0; i < n; i++) {
        	dp[i][0][0] = 0;
        }
        dp2[0][1][1] = 0;

        for (int i = 1; i < n; i++) {
        	for (int j = 1; j <= m; j++) {
        		if (i != n - 1) {
        			dp[i][j][0] = Math.max(dp[i][j][0], Math.max(dp[i - 1][j][0], dp[i - 1][j][1]));
        			dp[i][j][1] = Math.max(dp[i][j][1], Math.max(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] + w[i - 1]));
        			dp2[i][j][0] = Math.max(dp2[i][j][0], Math.max(dp2[i - 1][j][0], dp2[i - 1][j][1]));
        			dp2[i][j][1] = Math.max(dp2[i][j][1], Math.max(dp2[i - 1][j - 1][0], dp2[i - 1][j - 1][1] + w[i - 1]));
        		} else {
        			dp[i][j][0] = Math.max(dp[i][j][0], Math.max(dp[i - 1][j][0], dp[i - 1][j][1]));
        			dp[i][j][1] = Math.max(dp[i][j][1], Math.max(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1] + w[i - 1]));
        			dp2[i][j][0] = Math.max(dp2[i][j][0], Math.max(dp2[i - 1][j][0], dp2[i - 1][j][1]));
        			dp2[i][j][1] = Math.max(dp2[i][j][1], Math.max(dp2[i - 1][j - 1][0] + w[i], dp2[i - 1][j - 1][1] + w[i - 1] + w[i]));
        		}
        	}
        }

        int max = -INF;
        for (int i = 0; i < 2; i++) {
        	max = Math.max(max, dp[n - 1][m][i]);
        	max = Math.max(max, dp2[n - 1][m][i]);
        }

        pr.println(max);

        pr.close();
        sc.close();
    }

    @SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

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

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

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0