結果

問題 No.324 落ちてた閉路グラフ
ユーザー ぴろずぴろず
提出日時 2015-12-17 01:02:42
言語 Java19
(openjdk 21)
結果
AC  
実行時間 661 ms / 5,000 ms
コード長 1,622 bytes
コンパイル時間 4,231 ms
コンパイル使用メモリ 73,968 KB
実行使用メモリ 210,068 KB
最終ジャッジ日時 2023-08-09 21:27:17
合計ジャッジ時間 14,209 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,704 KB
testcase_01 AC 124 ms
55,968 KB
testcase_02 AC 123 ms
55,812 KB
testcase_03 AC 125 ms
55,740 KB
testcase_04 AC 466 ms
125,684 KB
testcase_05 AC 652 ms
209,612 KB
testcase_06 AC 415 ms
101,188 KB
testcase_07 AC 642 ms
210,068 KB
testcase_08 AC 397 ms
126,736 KB
testcase_09 AC 335 ms
77,056 KB
testcase_10 AC 450 ms
125,760 KB
testcase_11 AC 354 ms
76,932 KB
testcase_12 AC 124 ms
55,664 KB
testcase_13 AC 119 ms
55,968 KB
testcase_14 AC 255 ms
67,860 KB
testcase_15 AC 275 ms
78,596 KB
testcase_16 AC 120 ms
55,640 KB
testcase_17 AC 120 ms
55,488 KB
testcase_18 AC 125 ms
55,784 KB
testcase_19 AC 121 ms
55,928 KB
testcase_20 AC 119 ms
56,092 KB
testcase_21 AC 126 ms
55,580 KB
testcase_22 AC 121 ms
55,796 KB
testcase_23 AC 119 ms
55,784 KB
testcase_24 AC 121 ms
55,664 KB
testcase_25 AC 122 ms
57,612 KB
testcase_26 AC 123 ms
55,744 KB
testcase_27 AC 122 ms
55,800 KB
testcase_28 AC 121 ms
55,596 KB
testcase_29 AC 120 ms
55,564 KB
testcase_30 AC 121 ms
55,388 KB
testcase_31 AC 121 ms
55,592 KB
testcase_32 AC 661 ms
209,440 KB
testcase_33 AC 629 ms
209,268 KB
testcase_34 AC 457 ms
103,784 KB
testcase_35 AC 154 ms
55,972 KB
testcase_36 AC 289 ms
67,180 KB
testcase_37 AC 135 ms
56,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no324;

import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		if (m <= 1) {
			System.out.println(0);
			return;
		}
		int[] w = new int[n];
		for(int i=0;i<n;i++) {
			w[i] = sc.nextInt();
		}
		int[][][][] dp = new int[2][2][n][m+1]; //first, before, ind, count
		for(int i=0;i<2;i++) {
			for(int j=0;j<2;j++) {
				for(int k=0;k<n;k++) {
					Arrays.fill(dp[i][j][k], -(1<<29));
				}
			}
		}
		dp[0][0][0][0] = 0;
		dp[1][1][0][1] = 0;
		for(int i=0;i<n-1;i++) {
			for(int fir=0;fir<2;fir++) {
				for(int bef=0;bef<2;bef++) {
					for(int j=0;j<=m;j++) {
						if (dp[fir][bef][i][j] == -(1<<29)) {
							continue;
						}
						//取る
						if (j < m) {
							int nj = dp[fir][bef][i][j] + (bef == 1 ? w[i] : 0) + (i == n - 2 && fir == 1 ? w[n-1] : 0);
//							System.out.println(fir + "," + bef + "," + i + "," + j + ":" + dp[fir][bef][i][j] + "->" + fir + "," + 1 + "," + (i+1) + "," + (j+1) + ":" + nj);
							dp[fir][1][i+1][j+1] = Math.max(dp[fir][1][i+1][j+1], nj);
						}
						//取らない
						dp[fir][0][i+1][j] = Math.max(dp[fir][0][i+1][j], dp[fir][bef][i][j]);
//						System.out.println(fir + "," + bef + "," + i + "," + j + ":" + dp[fir][bef][i][j] + "->" + fir + "," + 0 + "," + (i+1) + "," + j + ":" + dp[fir][bef][i][j]);
					}
				}
			}
		}
		int ans = Integer.MIN_VALUE;
		for(int fir=0;fir<2;fir++) {
			for(int bef=0;bef<2;bef++) {
				ans = Math.max(ans, dp[fir][bef][n-1][m]);
			}
		}
		System.out.println(ans);
	}

}
0