結果

問題 No.324 落ちてた閉路グラフ
ユーザー 37zigen37zigen
提出日時 2016-03-22 00:52:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,010 ms / 5,000 ms
コード長 3,491 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 77,152 KB
実行使用メモリ 211,400 KB
最終ジャッジ日時 2024-04-27 16:09:20
合計ジャッジ時間 17,462 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 151 ms
54,392 KB
testcase_01 AC 143 ms
54,140 KB
testcase_02 AC 144 ms
54,036 KB
testcase_03 AC 142 ms
53,980 KB
testcase_04 AC 858 ms
127,552 KB
testcase_05 AC 887 ms
211,264 KB
testcase_06 AC 887 ms
101,248 KB
testcase_07 AC 1,010 ms
211,400 KB
testcase_08 AC 736 ms
104,620 KB
testcase_09 AC 582 ms
77,932 KB
testcase_10 AC 750 ms
105,208 KB
testcase_11 AC 606 ms
78,708 KB
testcase_12 AC 207 ms
56,484 KB
testcase_13 AC 200 ms
56,720 KB
testcase_14 AC 309 ms
66,944 KB
testcase_15 AC 410 ms
81,064 KB
testcase_16 AC 134 ms
53,976 KB
testcase_17 AC 133 ms
54,024 KB
testcase_18 AC 135 ms
53,964 KB
testcase_19 AC 132 ms
54,236 KB
testcase_20 AC 131 ms
53,972 KB
testcase_21 AC 133 ms
54,320 KB
testcase_22 AC 131 ms
53,812 KB
testcase_23 AC 133 ms
53,860 KB
testcase_24 AC 141 ms
53,820 KB
testcase_25 AC 146 ms
54,028 KB
testcase_26 AC 145 ms
54,348 KB
testcase_27 AC 147 ms
53,848 KB
testcase_28 AC 145 ms
54,300 KB
testcase_29 AC 146 ms
54,400 KB
testcase_30 AC 141 ms
53,936 KB
testcase_31 AC 144 ms
54,108 KB
testcase_32 AC 821 ms
211,132 KB
testcase_33 AC 909 ms
210,856 KB
testcase_34 AC 695 ms
108,832 KB
testcase_35 AC 162 ms
54,208 KB
testcase_36 AC 480 ms
69,680 KB
testcase_37 AC 143 ms
54,488 KB
権限があれば一括ダウンロードができます

ソースコード

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)){
								dp[i][l][k][j]=-10000000;
								continue;
							}else if(k==0){
								dp[i][l][k][j]=-10000000;
								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>=1){
									dp[i][l][k][j]=-10000000;
									continue;
								}else if(k==0){
									dp[i][l][k][j]=0;
								}
							}else if(k>j-2){
								dp[i][l][k][j]=-10000000;
								continue;
							}else if(m-k>n-j){
								dp[i][l][k][j]=-10000000;
								continue;//確定していない残り箇所がこれから塗らないといけない箇所の数より小さい
							}
						}else if(l==0&&i==1){
							if(!(k<=j-1)){
								dp[i][l][k][j]=-10000000;
								continue;
							}else if(k==0){
								dp[i][l][k][j]=-10000000;
								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