結果

問題 No.324 落ちてた閉路グラフ
ユーザー 37zigen37zigen
提出日時 2016-03-22 00:07:58
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,203 bytes
コンパイル時間 2,524 ms
コンパイル使用メモリ 79,140 KB
実行使用メモリ 167,200 KB
最終ジャッジ日時 2024-04-08 21:33:14
合計ジャッジ時間 10,540 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 207 ms
59,256 KB
testcase_01 AC 201 ms
59,272 KB
testcase_02 AC 205 ms
59,328 KB
testcase_03 AC 208 ms
59,272 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
		int[] w=new int[n];
		for(int i=0;i<n;i++){
			w[i]=sc.nextInt();
		}
		int[][][][] dp=new int[2][2][m+1][n+1];

		for(int i=0;i<2;i++){
			for(int j=0;j<2;j++){
				for(int k=0;k<m+1;k++){
					for(int l=0;l<n;l++){
						dp[i][j][k][l]=-1000000000;
					}
				}
			}
		}
		//n>=3
		for(int i=0;i<2;i++){//1番目の頂点を拾うかどうか
			for(int j=1;j<=n;j++){//j番目まで確定
				for(int k=0;k<=j&&k<=m;k++){//頂点数
					for(int l=0;l<2;l++){//i番目の頂点を拾うかどうか
						if(l==1&&i==0){
							//					1番目とi番目の頂点を拾う、拾わないの関係から、現在の頂点数に制約
							if(!(k<=j-1)){
								continue;
							}else if(k==0){
								continue;
							}else if(m-k>n-j){
								dp[i][l][k][j]=-10000000;
								continue;//確定していない残り箇所がこれから塗らないといけない箇所の数より小さい
							}
						}else if(l==1&&i==1){
							if(j==1&&k==1){//1番目しか確定しておらず、1着色
								dp[i][l][k][j]=0;
								continue;
							}else if(k<2){
								dp[i][l][k][j]=-1000000000;//着色される箇所が確定している箇所より多い
								continue;
							}else if(m-k>n-j){
								dp[i][l][k][j]=-10000000;
								continue;//確定していない残り箇所がこれから塗らないといけない箇所の数より小さい
							}
						}else if(l==0&&i==0){
							if(j==1){
								if(k>j-1)
									continue;
							}else if(k>j-2){
								continue;
							}else if(m-k>n-j){
								dp[i][l][k][j]=-10000000;
								continue;//確定していない残り箇所がこれから塗らないといけない箇所の数より小さい
							}
						}else if(l==0&&i==1){
							if(!(k<=j-1)){
								continue;
							}else if(k==0){
								continue;
							}else if(m-k>n-j){
								dp[i][l][k][j]=-10000000;
								continue;//確定していない残り箇所がこれから塗らないといけない箇所の数より小さい
							}
						}
						//					1番目の頂点での操作
						if(j==1){
							dp[i][l][k][j]=0;
						}else if(k==0){
							dp[i][l][k][j]=0;
	//	dp[1番目の頂点を拾うかどうか(i)][j番目の頂点を拾うかどうか(l)][頂点数k][jまで確定];
						}else if(l==0){
							dp[i][l][k][j]=Math.max(dp[i][0][k][j-1],dp[i][1][k][j-1]);
						}else if(l==1){
							dp[i][l][k][j]=Math.max(dp[i][1][k-1][j-1]+w[j-2],dp[i][0][k-1][j-1]);
						}

					}
				}
			}

		}

		dp[1][1][m][n]=dp[1][1][m][n]+w[n-1];
		int max=Math.max(dp[1][1][m][n],dp[1][0][m][n]);
		max=Math.max(max,dp[0][1][m][n]);
		max=Math.max(max, dp[0][0][m][n]);
		max=Math.max(max, dp[1][0][m][n]);

		System.out.println(max);



		for(int i=0;i<2;i++){//1番目の頂点を拾うかどうか
			for(int j=1;j<=n;j++){//j番目まで確定
				for(int k=0;k<=j&&k<=m;k++){//頂点数
					for(int l=0;l<2;l++){//i番目の頂点を拾うかどうか
							System.err.println("dp["+i+"]["+l+"]["+k+"]["+j+"]="+dp[i][l][k][j]);
					}
				}

			}

		}
	}
}
0