結果

問題 No.324 落ちてた閉路グラフ
ユーザー 37zigen37zigen
提出日時 2016-03-22 00:16:26
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,430 bytes
コンパイル時間 2,825 ms
コンパイル使用メモリ 77,196 KB
実行使用メモリ 215,052 KB
最終ジャッジ日時 2024-04-08 21:36:23
合計ジャッジ時間 18,937 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
57,816 KB
testcase_01 AC 132 ms
57,972 KB
testcase_02 AC 134 ms
57,712 KB
testcase_03 AC 131 ms
56,244 KB
testcase_04 AC 794 ms
131,380 KB
testcase_05 AC 871 ms
214,740 KB
testcase_06 AC 773 ms
104,800 KB
testcase_07 AC 995 ms
214,924 KB
testcase_08 AC 832 ms
108,928 KB
testcase_09 AC 621 ms
81,948 KB
testcase_10 AC 729 ms
108,856 KB
testcase_11 AC 632 ms
81,040 KB
testcase_12 AC 236 ms
60,340 KB
testcase_13 AC 226 ms
60,340 KB
testcase_14 AC 342 ms
71,036 KB
testcase_15 AC 442 ms
83,716 KB
testcase_16 AC 134 ms
57,948 KB
testcase_17 AC 137 ms
57,956 KB
testcase_18 AC 133 ms
55,752 KB
testcase_19 AC 139 ms
57,776 KB
testcase_20 AC 134 ms
57,840 KB
testcase_21 AC 138 ms
57,736 KB
testcase_22 AC 133 ms
57,736 KB
testcase_23 WA -
testcase_24 AC 134 ms
57,960 KB
testcase_25 AC 133 ms
57,980 KB
testcase_26 AC 135 ms
58,192 KB
testcase_27 WA -
testcase_28 AC 133 ms
57,852 KB
testcase_29 AC 138 ms
57,836 KB
testcase_30 AC 134 ms
57,940 KB
testcase_31 AC 137 ms
57,684 KB
testcase_32 AC 824 ms
214,576 KB
testcase_33 AC 875 ms
215,052 KB
testcase_34 WA -
testcase_35 AC 202 ms
59,820 KB
testcase_36 AC 506 ms
73,108 KB
testcase_37 AC 155 ms
57,976 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>j-1)
									dp[i][l][k][j]=-10000000;
									continue;
							}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