結果

問題 No.324 落ちてた閉路グラフ
ユーザー 37zigen37zigen
提出日時 2016-03-21 23:52:01
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,349 bytes
コンパイル時間 2,389 ms
コンパイル使用メモリ 79,684 KB
実行使用メモリ 212,436 KB
最終ジャッジ日時 2024-04-08 21:32:56
合計ジャッジ時間 15,932 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
57,832 KB
testcase_01 AC 142 ms
58,176 KB
testcase_02 AC 144 ms
58,224 KB
testcase_03 AC 143 ms
57,704 KB
testcase_04 AC 642 ms
128,412 KB
testcase_05 AC 876 ms
212,116 KB
testcase_06 AC 630 ms
99,824 KB
testcase_07 AC 863 ms
212,112 KB
testcase_08 AC 595 ms
105,900 KB
testcase_09 AC 426 ms
79,268 KB
testcase_10 AC 641 ms
105,876 KB
testcase_11 AC 489 ms
79,208 KB
testcase_12 AC 224 ms
60,644 KB
testcase_13 AC 217 ms
60,356 KB
testcase_14 AC 354 ms
71,460 KB
testcase_15 WA -
testcase_16 AC 137 ms
57,936 KB
testcase_17 AC 140 ms
57,720 KB
testcase_18 AC 144 ms
57,704 KB
testcase_19 AC 142 ms
57,688 KB
testcase_20 AC 141 ms
57,704 KB
testcase_21 AC 138 ms
57,704 KB
testcase_22 WA -
testcase_23 AC 141 ms
57,724 KB
testcase_24 AC 140 ms
57,788 KB
testcase_25 AC 140 ms
57,940 KB
testcase_26 AC 142 ms
57,816 KB
testcase_27 AC 139 ms
58,092 KB
testcase_28 AC 137 ms
57,960 KB
testcase_29 AC 141 ms
57,684 KB
testcase_30 AC 137 ms
57,964 KB
testcase_31 AC 137 ms
57,968 KB
testcase_32 AC 864 ms
212,044 KB
testcase_33 AC 809 ms
212,436 KB
testcase_34 AC 592 ms
109,660 KB
testcase_35 AC 188 ms
58,700 KB
testcase_36 AC 431 ms
68,200 KB
testcase_37 AC 148 ms
57,828 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))
								continue;
							if(k==0)
								continue;
						}else if(l==1&&i==1){
							if(j==1&&k==1){
								dp[i][l][k][j]=0;
								continue;
							}else if(k<2){
								dp[i][l][k][j]=-1000000000;
								continue;
							}
						}else if(l==0&&i==0){
							if(j==1){
								if(k>j-1)
									continue;
							}else if(k>j-2){
								continue;
							}
						}else if(l==0&&i==1){
							if(!(k<=j-1))
								continue;
							if(k==0)
								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